Skip to content

Commit 4fa6508

Browse files
committed
Fix PATH env variable when spawn process
1 parent 2701ac4 commit 4fa6508

File tree

3 files changed

+29
-31
lines changed

3 files changed

+29
-31
lines changed

azure-pipelines.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
jobs:
22

33
- job: Windows
4-
pool: 'Hosted VS2017'
4+
pool:
5+
vmImage: 'vs2017-win2016'
56

67
variables:
78
os_name: Windows

packages/berry-core/sources/scriptUtils.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {CwdFS, ZipOpenFS, xfs} from '@berry/fslib';
1+
import {CwdFS, ZipOpenFS, xfs, NodeFS} from '@berry/fslib';
22
import {execute} from '@berry/shell';
33
import {delimiter, posix} from 'path';
44
import {PassThrough, Readable, Writable} from 'stream';
@@ -22,16 +22,19 @@ async function makePathWrapper(location: string, name: string, argv0: string, ar
2222
}
2323

2424
export async function makeScriptEnv(project: Project) {
25-
const scriptEnv = {... process.env};
25+
const scriptEnv: {[key: string]: string} = {};
26+
for (const key of Object.keys(process.env))
27+
scriptEnv[key.toUpperCase()] = process.env[key] as string;
28+
2629
const binFolder = scriptEnv.BERRY_BIN_FOLDER = dirSync().name;
2730

2831
// Register some binaries that must be made available in all subprocesses
2932
// spawned by Berry
3033

31-
await makePathWrapper(binFolder, `run`, process.execPath, [process.argv[1], `run`]),
32-
await makePathWrapper(binFolder, `yarn`, process.execPath, [process.argv[1]]),
33-
await makePathWrapper(binFolder, `yarnpkg`, process.execPath, [process.argv[1]]),
34-
await makePathWrapper(binFolder, `node`, process.execPath),
34+
await makePathWrapper(binFolder, `run`, process.execPath, [process.argv[1], `run`]);
35+
await makePathWrapper(binFolder, `yarn`, process.execPath, [process.argv[1]]);
36+
await makePathWrapper(binFolder, `yarnpkg`, process.execPath, [process.argv[1]]);
37+
await makePathWrapper(binFolder, `node`, process.execPath);
3538

3639
scriptEnv.PATH = scriptEnv.PATH
3740
? `${binFolder}${delimiter}${scriptEnv.PATH}`
@@ -40,7 +43,7 @@ export async function makeScriptEnv(project: Project) {
4043
// Add the .pnp.js file to the Node options, so that we're sure that PnP will
4144
// be correctly setup
4245

43-
const pnpPath = `${project.cwd}/.pnp.js`;
46+
const pnpPath = NodeFS.fromPortablePath(`${project.cwd}/.pnp.js`);
4447
const pnpRequire = `--require ${pnpPath}`;
4548

4649
if (xfs.existsSync(pnpPath)) {
@@ -144,7 +147,7 @@ type GetPackageAccessibleBinariesOptions = {
144147

145148
/**
146149
* Return the binaries that can be accessed by the specified package
147-
*
150+
*
148151
* @param locator The queried package
149152
* @param project The project owning the package
150153
*/
@@ -161,43 +164,43 @@ export async function getPackageAccessibleBinaries(locator: Locator, {project}:
161164

162165
const linkers = project.configuration.getLinkers();
163166
const linkerOptions = {project, report: new StreamReport({ configuration, stdout })};
164-
167+
165168
const binaries: Map<string, [Locator, string]> = new Map();
166-
169+
167170
const descriptors = [
168171
... pkg.dependencies.values(),
169172
... pkg.peerDependencies.values(),
170173
];
171-
174+
172175
for (const descriptor of descriptors) {
173176
const resolution = project.storedResolutions.get(descriptor.descriptorHash);
174177
if (!resolution)
175178
continue;
176-
179+
177180
const pkg = project.storedPackages.get(resolution);
178181
if (!pkg)
179182
continue;
180-
183+
181184
const linker = linkers.find(linker => linker.supportsPackage(pkg, linkerOptions));
182185
if (!linker)
183186
continue;
184-
187+
185188
const packageLocation = await linker.findPackageLocation(pkg, linkerOptions);
186189
const packageFs = new CwdFS(packageLocation, {baseFs: zipOpenFs});
187190
const manifest = await Manifest.find(`.`, {baseFs: packageFs});
188-
191+
189192
for (const [binName, file] of manifest.bin.entries()) {
190193
binaries.set(binName, [pkg, posix.resolve(packageLocation, file)]);
191194
}
192195
}
193-
196+
194197
return binaries;
195198
});
196199
}
197200

198201
/**
199202
* Return the binaries that can be accessed by the specified workspace
200-
*
203+
*
201204
* @param workspace The queried workspace
202205
*/
203206

@@ -215,11 +218,11 @@ type ExecutePackageAccessibleBinaryOptions = {
215218

216219
/**
217220
* Execute a binary from the specified package.
218-
*
221+
*
219222
* Note that "binary" in this sense means "a Javascript file". Actual native
220223
* binaries cannot be executed this way, because we use Node in order to
221224
* transparently read from the archives.
222-
*
225+
*
223226
* @param locator The queried package
224227
* @param binaryName The name of the binary file to execute
225228
* @param args The arguments to pass to the file
@@ -254,7 +257,7 @@ type ExecuteWorkspaceAccessibleBinaryOptions = {
254257

255258
/**
256259
* Execute a binary from the specified workspace
257-
*
260+
*
258261
* @param workspace The queried package
259262
* @param binaryName The name of the binary file to execute
260263
* @param args The arguments to pass to the file

packages/berry-shell/sources/index.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {xfs, NodeFS} from '@berry/fslib';
1+
import {xfs, NodeFS} from '@berry/fslib';
22
import {CommandSegment, CommandChain, CommandLine, ShellLine, parseShell} from '@berry/parsers';
33
import {spawn} from 'child_process';
44
import {delimiter, posix} from 'path';
@@ -133,13 +133,10 @@ const BUILTINS = new Map<string, ShellBuiltin>([
133133
for (const key of Object.keys(state.environment))
134134
normalizedEnv[key.toUpperCase()] = state.environment[key] as string;
135135

136-
// We must make sure the PATH is properly converted into the
137-
// system-specific format
138-
if (normalizedEnv.PATH && posix.delimiter !== delimiter)
139-
normalizedEnv.PATH = normalizedEnv.PATH.replace(new RegExp(posix.delimiter, `g`), delimiter);
140136

141137
const subprocess = spawn(ident, rest, {
142138
cwd: NodeFS.fromPortablePath(state.cwd),
139+
shell: process.platform === `win32`, // Needed to execute .cmd files
143140
env: normalizedEnv,
144141
stdio,
145142
});
@@ -566,11 +563,8 @@ export async function execute(command: string, args: Array<string> = [], {
566563

567564
if (paths.length > 0)
568565
normalizedEnv.PATH = normalizedEnv.PATH
569-
? `${paths.join(posix.delimiter)}${posix.delimiter}${env.PATH}`
570-
: `${paths.join(posix.delimiter)}`;
571-
572-
if (normalizedEnv.PATH && delimiter !== posix.delimiter)
573-
normalizedEnv.PATH = normalizedEnv.PATH.replace(new RegExp(delimiter, `g`), posix.delimiter);
566+
? `${paths.join(delimiter)}${delimiter}${env.PATH}`
567+
: `${paths.join(delimiter)}`;
574568

575569
const normalizedBuiltins = new Map(BUILTINS);
576570
for (const [key, action] of Object.entries(builtins))

0 commit comments

Comments
 (0)