Skip to content

Commit

Permalink
Fix PATH env variable when spawn process
Browse files Browse the repository at this point in the history
  • Loading branch information
Embraser01 committed Mar 22, 2019
1 parent 2701ac4 commit 4fa6508
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 31 deletions.
3 changes: 2 additions & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
jobs:

- job: Windows
pool: 'Hosted VS2017'
pool:
vmImage: 'vs2017-win2016'

variables:
os_name: Windows
Expand Down
43 changes: 23 additions & 20 deletions packages/berry-core/sources/scriptUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {CwdFS, ZipOpenFS, xfs} from '@berry/fslib';
import {CwdFS, ZipOpenFS, xfs, NodeFS} from '@berry/fslib';
import {execute} from '@berry/shell';
import {delimiter, posix} from 'path';
import {PassThrough, Readable, Writable} from 'stream';
Expand All @@ -22,16 +22,19 @@ async function makePathWrapper(location: string, name: string, argv0: string, ar
}

export async function makeScriptEnv(project: Project) {
const scriptEnv = {... process.env};
const scriptEnv: {[key: string]: string} = {};
for (const key of Object.keys(process.env))
scriptEnv[key.toUpperCase()] = process.env[key] as string;

const binFolder = scriptEnv.BERRY_BIN_FOLDER = dirSync().name;

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

await makePathWrapper(binFolder, `run`, process.execPath, [process.argv[1], `run`]),
await makePathWrapper(binFolder, `yarn`, process.execPath, [process.argv[1]]),
await makePathWrapper(binFolder, `yarnpkg`, process.execPath, [process.argv[1]]),
await makePathWrapper(binFolder, `node`, process.execPath),
await makePathWrapper(binFolder, `run`, process.execPath, [process.argv[1], `run`]);
await makePathWrapper(binFolder, `yarn`, process.execPath, [process.argv[1]]);
await makePathWrapper(binFolder, `yarnpkg`, process.execPath, [process.argv[1]]);
await makePathWrapper(binFolder, `node`, process.execPath);

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

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

if (xfs.existsSync(pnpPath)) {
Expand Down Expand Up @@ -144,7 +147,7 @@ type GetPackageAccessibleBinariesOptions = {

/**
* Return the binaries that can be accessed by the specified package
*
*
* @param locator The queried package
* @param project The project owning the package
*/
Expand All @@ -161,43 +164,43 @@ export async function getPackageAccessibleBinaries(locator: Locator, {project}:

const linkers = project.configuration.getLinkers();
const linkerOptions = {project, report: new StreamReport({ configuration, stdout })};

const binaries: Map<string, [Locator, string]> = new Map();

const descriptors = [
... pkg.dependencies.values(),
... pkg.peerDependencies.values(),
];

for (const descriptor of descriptors) {
const resolution = project.storedResolutions.get(descriptor.descriptorHash);
if (!resolution)
continue;

const pkg = project.storedPackages.get(resolution);
if (!pkg)
continue;

const linker = linkers.find(linker => linker.supportsPackage(pkg, linkerOptions));
if (!linker)
continue;

const packageLocation = await linker.findPackageLocation(pkg, linkerOptions);
const packageFs = new CwdFS(packageLocation, {baseFs: zipOpenFs});
const manifest = await Manifest.find(`.`, {baseFs: packageFs});

for (const [binName, file] of manifest.bin.entries()) {
binaries.set(binName, [pkg, posix.resolve(packageLocation, file)]);
}
}

return binaries;
});
}

/**
* Return the binaries that can be accessed by the specified workspace
*
*
* @param workspace The queried workspace
*/

Expand All @@ -215,11 +218,11 @@ type ExecutePackageAccessibleBinaryOptions = {

/**
* Execute a binary from the specified package.
*
*
* Note that "binary" in this sense means "a Javascript file". Actual native
* binaries cannot be executed this way, because we use Node in order to
* transparently read from the archives.
*
*
* @param locator The queried package
* @param binaryName The name of the binary file to execute
* @param args The arguments to pass to the file
Expand Down Expand Up @@ -254,7 +257,7 @@ type ExecuteWorkspaceAccessibleBinaryOptions = {

/**
* Execute a binary from the specified workspace
*
*
* @param workspace The queried package
* @param binaryName The name of the binary file to execute
* @param args The arguments to pass to the file
Expand Down
14 changes: 4 additions & 10 deletions packages/berry-shell/sources/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {xfs, NodeFS} from '@berry/fslib';
import {xfs, NodeFS} from '@berry/fslib';
import {CommandSegment, CommandChain, CommandLine, ShellLine, parseShell} from '@berry/parsers';
import {spawn} from 'child_process';
import {delimiter, posix} from 'path';
Expand Down Expand Up @@ -133,13 +133,10 @@ const BUILTINS = new Map<string, ShellBuiltin>([
for (const key of Object.keys(state.environment))
normalizedEnv[key.toUpperCase()] = state.environment[key] as string;

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

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

if (paths.length > 0)
normalizedEnv.PATH = normalizedEnv.PATH
? `${paths.join(posix.delimiter)}${posix.delimiter}${env.PATH}`
: `${paths.join(posix.delimiter)}`;

if (normalizedEnv.PATH && delimiter !== posix.delimiter)
normalizedEnv.PATH = normalizedEnv.PATH.replace(new RegExp(delimiter, `g`), posix.delimiter);
? `${paths.join(delimiter)}${delimiter}${env.PATH}`
: `${paths.join(delimiter)}`;

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

0 comments on commit 4fa6508

Please sign in to comment.