Skip to content

Commit 74f4435

Browse files
Justin Chasejasnell
Justin Chase
authored andcommitted
module: use UNC paths when loading native addons
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. PR-URL: #2965 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 46876d5 commit 74f4435

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

lib/module.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,9 @@ Module._extensions['.json'] = function(module, filename) {
456456

457457

458458
//Native extension for .node
459-
Module._extensions['.node'] = process.dlopen;
459+
Module._extensions['.node'] = function(module, filename) {
460+
return process.dlopen(module, path._makeLong(filename));
461+
};
460462

461463

462464
// bootstrap main module.

test/addons/load-long-path/binding.cc

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <node.h>
2+
#include <v8.h>
3+
4+
void Method(const v8::FunctionCallbackInfo<v8::Value>& args) {
5+
v8::Isolate* isolate = args.GetIsolate();
6+
args.GetReturnValue().Set(v8::String::NewFromUtf8(isolate, "world"));
7+
}
8+
9+
void init(v8::Local<v8::Object> target) {
10+
NODE_SET_METHOD(target, "hello", Method);
11+
}
12+
13+
NODE_MODULE(binding, init);
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
'targets': [
3+
{
4+
'target_name': 'binding',
5+
'sources': [ 'binding.cc' ]
6+
}
7+
]
8+
}

test/addons/load-long-path/test.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
const common = require('../../common');
3+
const fs = require('fs');
4+
const path = require('path');
5+
const assert = require('assert');
6+
7+
common.refreshTmpDir();
8+
9+
// make a path that is more than 260 chars long.
10+
// Any given folder cannot have a name longer than 260 characters,
11+
// so create 10 nested folders each with 30 character long names.
12+
var addonDestinationDir = path.resolve(common.tmpDir);
13+
14+
for (var i = 0; i < 10; i++) {
15+
addonDestinationDir = path.join(addonDestinationDir, 'x'.repeat(30));
16+
fs.mkdirSync(addonDestinationDir);
17+
}
18+
19+
const addonPath = path.join(__dirname, 'build', 'Release', 'binding.node');
20+
const addonDestinationPath = path.join(addonDestinationDir, 'binding.node');
21+
22+
// Copy binary to long path destination
23+
var contents = fs.readFileSync(addonPath);
24+
fs.writeFileSync(addonDestinationPath, contents);
25+
26+
// Attempt to load at long path destination
27+
var addon = require(addonDestinationPath);
28+
assert(addon != null);
29+
assert(addon.hello() == 'world');

0 commit comments

Comments
 (0)