fix(bazel): Bazel builder resolves with require.resolve() (#28478)

This commit fixes a bug in the Bazel builder in which the path to Bazel
executable is constructed using the project path. For non-default
project, the node_modules directory is actually one level above the
project path.

This PR fixes the bug by resolving node_modules with require.resolve().
It requires @bazel/bazel v0.22.1 because previous versions do not have
index.js or main field in package.json and would cause node module
resolution to fail.

This has been tested with both bazel and ibazel.

PR Close #28478
This commit is contained in:
Keen Yee Liau
2019-01-30 17:11:03 -08:00
committed by Matias Niemelä
parent 1a25144297
commit d85d396c26
4 changed files with 29 additions and 26 deletions

View File

@ -8,7 +8,6 @@
/// <reference types='node'/>
import {spawn, spawnSync} from 'child_process';
import {join} from 'path';
import {Observable, Subject} from 'rxjs';
export type Executable = 'bazel' | 'ibazel';
@ -18,7 +17,7 @@ export function runBazel(
projectDir: string, executable: Executable, command: Command, workspaceTarget: string,
flags: string[]): Observable<void> {
const doneSubject = new Subject<void>();
const bin = join(projectDir, 'node_modules', '.bin', executable);
const bin = require.resolve(`@bazel/${executable}`);
const buildProcess = spawn(bin, [command, workspaceTarget, ...flags], {
cwd: projectDir,
stdio: 'inherit',
@ -37,7 +36,12 @@ export function runBazel(
}
export function checkInstallation(executable: Executable, projectDir: string) {
const bin = join(projectDir, 'node_modules', '.bin', executable);
let bin: string;
try {
bin = require.resolve(`@bazel/${executable}`);
} catch {
return false;
}
const child = spawnSync(bin, ['version'], {
cwd: projectDir,
shell: false,

View File

@ -41,7 +41,7 @@ function addDevDependenciesToPackageJson(options: Schema) {
const devDependencies: {[k: string]: string} = {
'@angular/bazel': angularCoreVersion,
// TODO(kyliau): Consider moving this to latest-versions.ts
'@bazel/bazel': '^0.21.0',
'@bazel/bazel': '^0.22.1',
'@bazel/ibazel': '^0.9.0',
'@bazel/karma': '^0.22.1',
'@bazel/typescript': '^0.22.1',