Skip to content
Merged
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
74 changes: 74 additions & 0 deletions e2e/harmony/dependencies/node-gyp.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import chai, { expect } from 'chai';
import fs from 'fs';
import path from 'path';
import { Helper, NpmCiRegistry, supportNpmCiRegistryTesting } from '@teambit/legacy.e2e-helper';
import chaiFs from 'chai-fs';
chai.use(chaiFs);

/** The env keys naming PATH, as this platform spells them — `Path` on Windows. */
function pathEnvKeys(): string[] {
const keys = Object.keys(process.env).filter((key) => key.toUpperCase() === 'PATH');
return keys.length ? keys : ['PATH'];
}

/**
* A PATH override holding this process's PATH minus every directory with a
* `node-gyp` in it, so the installed Bit has to supply its own. Without it the
* test would pass either way: `npm run` puts the repo's `node_modules/.bin` —
* which carries a `node-gyp` bin link — on PATH, and dependency build scripts
* inherit it.
*
* Keyed by the casing already in `process.env`, because the command helper
* spreads it into a plain object: an override under a casing of its own would
* sit alongside the original rather than replacing it.
*/
function pathWithoutNodeGyp(): Record<string, string> {
const keys = pathEnvKeys();
const filtered = (process.env[keys[0]] ?? '')
.split(path.delimiter)
.filter((dir) => !['node-gyp', 'node-gyp.cmd'].some((bin) => fs.existsSync(path.join(dir, bin))))
.join(path.delimiter);
return Object.fromEntries(keys.map((key) => [key, filtered]));
}

// The pnpm engine ships no node-gyp and spawns dependency build scripts with
// Bit's own PATH, so without the node-gyp Bit puts there, `node-gyp rebuild`
// dies with "node-gyp: command not found".
(supportNpmCiRegistryTesting ? describe : describe.skip)('a dependency built by node-gyp', function () {
this.timeout(0);
let helper: Helper;
let npmCiRegistry: NpmCiRegistry;
before(async () => {
helper = new Helper({ scopesOptions: { remoteScopeWithDot: true } });
helper.scopeHelper.setWorkspaceWithRemoteScope();
helper.workspaceJsonc.setPackageManager(`teambit.dependencies/pnpm`);
npmCiRegistry = new NpmCiRegistry(helper);
await npmCiRegistry.init();

npmCiRegistry.setRegistry();
// A store of its own, so the package is built by this install rather than
// restored from the side-effects cache of an earlier one — which would
// reproduce the build output without ever running node-gyp.
fs.writeFileSync(
path.join(helper.fixtures.scopes.localPath, 'pnpm-workspace.yaml'),
`storeDir: ${path.join(helper.fixtures.scopes.localPath, '.pnpm-store')}\n`
);
helper.extensions.workspaceJsonc.addKeyValToDependencyResolver('allowScripts', {
'@pnpm.e2e/has-binding-gyp': true,
});
helper.command.install('@pnpm.e2e/has-binding-gyp', undefined, undefined, {
envVariables: pathWithoutNodeGyp(),
});
Comment on lines +59 to +61

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Remediation recommended

1. Path override not portable 🐞 Bug ☼ Reliability

The new node-gyp e2e test only reads/overrides PATH, but CommandHelper.runCmd() builds the
child-process env by spreading process.env into a plain object and does not normalize/remove
alternate-cased path keys, so an existing Path entry (common on Windows) may remain unfiltered
alongside the new PATH. This can make the test’s “no node-gyp on PATH” guarantee unreliable on
Windows, potentially hiding regressions.
Agent Prompt
### Issue description
The new e2e test tries to remove all existing `node-gyp` locations from the PATH before running `bit install`, but it only reads `process.env.PATH` and only passes `envVariables: { PATH: ... }`. On Windows, the effective path variable may be represented as `Path` (different casing), and the helper builds an `env` plain object from `process.env` + overrides without normalizing/deleting alternate casings.

### Issue Context
This can cause the test to still run with an unfiltered PATH (e.g., `Path` preserved) even though `PATH` was overridden, making the test nondeterministic on Windows and potentially allowing a false pass when Bit fails to supply node-gyp.

### Fix Focus Areas
- e2e/harmony/dependencies/node-gyp.e2e.ts[14-18]
- e2e/harmony/dependencies/node-gyp.e2e.ts[46-48]

### Suggested fix
1. Update `pathWithoutNodeGyp()` to read from `process.env.PATH || process.env.Path || process.env.path || ''`.
2. When invoking `helper.command.install(...)`, set all common casings to the same filtered value, e.g.:
   - `envVariables: { PATH: filtered, Path: filtered, path: filtered }`

This keeps the fix local to the new test and makes it robust across platforms.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

});
after(() => {
npmCiRegistry.destroy();
helper.scopeHelper.destroy();
});
it('should run its install script through node-gyp', () => {
// Written by the gyp action in the package's binding.gyp, so it only exists
// if `node-gyp rebuild` was found and ran.
expect(
path.join(helper.fixtures.scopes.localPath, 'node_modules/@pnpm.e2e/has-binding-gyp/generated.js')
).to.be.a.path();
});
});
Loading