Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
lib,test: Use long paths when loading native addons (windows only)
When using require to load a native addon the path must be converted
into a long path, otherwise the addon will fail to be loaded on
windows if the path is longer than 260 characters.
  • Loading branch information
Justin Chase authored and justinmchase committed Sep 30, 2015
commit 450d406649768dc50d9517f02e688eb4810a199a
4 changes: 3 additions & 1 deletion lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,9 @@ Module._extensions['.json'] = function(module, filename) {


//Native extension for .node
Module._extensions['.node'] = process.dlopen;
Module._extensions['.node'] = function(module, filename) {
return process.dlopen(module, path._makeLong(filename));
};


// bootstrap main module.
Expand Down
14 changes: 14 additions & 0 deletions test/addons/load-long-path/binding.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <node.h>
#include <v8.h>

void Method(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::Isolate* isolate = args.GetIsolate();
v8::HandleScope scope(isolate);
args.GetReturnValue().Set(v8::String::NewFromUtf8(isolate, "world"));
}

void init(v8::Local<v8::Object> target) {
NODE_SET_METHOD(target, "hello", Method);
}

NODE_MODULE(binding, init);
8 changes: 8 additions & 0 deletions test/addons/load-long-path/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
'targets': [
{
'target_name': 'binding',
'sources': [ 'binding.cc' ]
}
]
}
29 changes: 29 additions & 0 deletions test/addons/load-long-path/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';
const common = require('../../common');
const fs = require('fs');
const path = require('path');
const assert = require('assert');

common.refreshTmpDir();

// make a path that is more than 260 chars long.
// Any given folder cannot have a name longer than 260 characters,
// so create 10 nested folders each with 30 character long names.
var addonDestinationDir = path.resolve(common.tmpDir);

for (var i = 0; i < 10; i++) {
addonDestinationDir = path.join(addonDestinationDir, 'x'.repeat(30));
fs.mkdirSync(addonDestinationDir);
}

const addonPath = path.join(__dirname, 'build', 'Release', 'binding.node');
const addonDestinationPath = path.join(addonDestinationDir, 'binding.node');

// Copy binary to long path destination
var contents = fs.readFileSync(addonPath);
fs.writeFileSync(addonDestinationPath, contents);

// Attempt to load at long path destination
var addon = require(addonDestinationPath);
assert(addon != null);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you assert.equal(addon.hello(), 'world'); here?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@justinmchase Are you intentionally comparing against both null and undefined here?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thefourtheye I believe I just copied that line from another test doing something similar. However, I think its most accurate to say that addon should be neither null or undefined. So yes it is intentional.

Also, the error case before this PR actually throws an error instead of returning null or undefined. So it's mostly just a sanity check. Same with the following line which actually calls the addon.hello() function, another sanity check.

assert(addon.hello() == 'world');