browserify / resolve

Implements the node.js require.resolve() algorithm
MIT License
776 stars 184 forks source link

Incorrect work on Linux (`Error: `resolve` must be run directly as an executable`) #316

Closed vvscode closed 1 year ago

vvscode commented 1 year ago

As continue to https://github.com/browserify/resolve/issues/315

I faced a problem, when locally(on Mac) everything works, and on CI (Linux) - not I got Error:resolvemust be run directly as an executable

after some investigation I discovered that

// DEBUG PATCH
console.log('@@resolve debug info:', JSON.stringify({
    npm_lifecycle_script: process.env.npm_lifecycle_script,
    argv: process.argv,
    __filename,
    ino1: fs.statSync(process.argv[1]).ino,
    ino2: fs.statSync(__filename).ino,
    _: process.env._,
    realpathSync: fs.realpathSync(path.resolve(process.env._))
}, null, 2), { version: pkg.version});

// ---------

if (
    String(process.env.npm_lifecycle_script).slice(0, 8) !== 'resolve '
    && (
        !process.argv
        || process.argv.length < 2
        || (process.argv[1] !== __filename && fs.statSync(process.argv[1]).ino !== fs.statSync(__filename).ino)
        || (process.env._ && fs.realpathSync(path.resolve(process.env._)) !== __filename)
    )
) {
    console.error('Error: `resolve` must be run directly as an executable');
    process.exit(1);
}

gives me different results in section _ (process.env._)

On Mac (locally):

➜  tmp npx resolve 123
@@resolve debug info: {
  "npm_lifecycle_script": "resolve",
  "argv": [
    "/opt/homebrew/Cellar/node/20.6.0/bin/node",
    "/Users/v.vanchuk/repo/tmp/node_modules/.bin/resolve",
    "123"
  ],
  "__filename": "/Users/v.vanchuk/repo/tmp/node_modules/resolve-with-debug/bin/resolve",
  "ino1": 4677520,
  "ino2": 4677520,
  "_": "/Users/v.vanchuk/repo/tmp/node_modules/.bin/resolve",
  "realpathSync": "/Users/v.vanchuk/repo/tmp/node_modules/resolve-with-debug/bin/resolve"
} { version: '1.22.10' }
/Users/v.vanchuk/repo/tmp/node_modules/resolve-with-debug/lib/sync.js:115
    throw err;
    ^

Error: Cannot find module '123' from '/Users/v.vanchuk/repo/tmp'

On Linux (I used github codespaces to reproduce):

@vvscode ➜ /workspaces/codespaces-blank $ npx resolve 123
@@resolve debug info: {
  "npm_lifecycle_script": "resolve",
  "argv": [
    "/usr/local/share/nvm/versions/node/v20.6.1/bin/node",
    "/workspaces/codespaces-blank/node_modules/.bin/resolve",
    "123"
  ],
  "__filename": "/workspaces/codespaces-blank/node_modules/resolve-with-debug/bin/resolve",
  "ino1": 1453838,
  "ino2": 1453838,
  "_": "/home/codespace/nvm/current/bin/npx",
  "realpathSync": "/usr/local/share/nvm/versions/node/v20.6.1/lib/node_modules/npm/bin/npx-cli.js"
} { version: '1.22.10' }
Error: `resolve` must be run directly as an executable

Steps to reproduce: