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 ec6e7303dd
commit 36902e2f0e
4 changed files with 29 additions and 26 deletions

View File

@ -10,8 +10,7 @@ function testBazel() {
ng new demo --collection=@angular/bazel --defaults --skip-git --style=scss ng new demo --collection=@angular/bazel --defaults --skip-git --style=scss
node replace_angular_repo.js "./demo/WORKSPACE" node replace_angular_repo.js "./demo/WORKSPACE"
cd demo cd demo
# TODO(kyliau) pin @angular/bazel version to 7.2.2 due to builder bug in 7.2.3 yarn add @angular/bazel@file:../../../dist/packages-dist/bazel
yarn add @angular/bazel@7.2.2
cp ../package.json.replace ./package.json cp ../package.json.replace ./package.json
ng generate component widget --style=css ng generate component widget --style=css
ng build ng build

View File

@ -51,7 +51,7 @@
rxjs "6.3.3" rxjs "6.3.3"
source-map "0.7.3" source-map "0.7.3"
"@angular-devkit/schematics@7.1.3", "@angular-devkit/schematics@^7.0.4": "@angular-devkit/schematics@7.1.3":
version "7.1.3" version "7.1.3"
resolved "https://registry.yarnpkg.com/@angular-devkit/schematics/-/schematics-7.1.3.tgz#30d03fde5bb27d6606d9a6e055188382408670d6" resolved "https://registry.yarnpkg.com/@angular-devkit/schematics/-/schematics-7.1.3.tgz#30d03fde5bb27d6606d9a6e055188382408670d6"
integrity sha512-Snmfog/n5k1PWdDaI+Top1F978vlXZFTvxHRPzlMCGhGsY+LMOpeRLVHADI+WP1q1LZ+2BjLELZVA2GP35AH8A== integrity sha512-Snmfog/n5k1PWdDaI+Top1F978vlXZFTvxHRPzlMCGhGsY+LMOpeRLVHADI+WP1q1LZ+2BjLELZVA2GP35AH8A==
@ -59,7 +59,7 @@
"@angular-devkit/core" "7.1.3" "@angular-devkit/core" "7.1.3"
rxjs "6.3.3" rxjs "6.3.3"
"@angular-devkit/schematics@7.3.0-rc.0": "@angular-devkit/schematics@7.3.0-rc.0", "@angular-devkit/schematics@^7.3.0-rc.0":
version "7.3.0-rc.0" version "7.3.0-rc.0"
resolved "https://registry.yarnpkg.com/@angular-devkit/schematics/-/schematics-7.3.0-rc.0.tgz#9f1e1f6942da36b12c81241398ed6ca8b2e65875" resolved "https://registry.yarnpkg.com/@angular-devkit/schematics/-/schematics-7.3.0-rc.0.tgz#9f1e1f6942da36b12c81241398ed6ca8b2e65875"
integrity sha512-noqcQIOvah2G126DTFKY5Kiga8UwI9cKzyhQdNlf+8hAZpnWwTURItQ5xuMJg/XfRQLUSg9gWS2h1cI9AD7mxQ== integrity sha512-noqcQIOvah2G126DTFKY5Kiga8UwI9cKzyhQdNlf+8hAZpnWwTURItQ5xuMJg/XfRQLUSg9gWS2h1cI9AD7mxQ==
@ -68,11 +68,11 @@
rxjs "6.3.3" rxjs "6.3.3"
"@angular/bazel@file:../../dist/packages-dist/bazel": "@angular/bazel@file:../../dist/packages-dist/bazel":
version "8.0.0-beta.0" version "8.0.0-beta.1"
dependencies: dependencies:
"@angular-devkit/architect" "^0.10.6" "@angular-devkit/architect" "^0.10.6"
"@angular-devkit/core" "^7.0.4" "@angular-devkit/core" "^7.0.4"
"@angular-devkit/schematics" "^7.0.4" "@angular-devkit/schematics" "^7.3.0-rc.0"
"@bazel/typescript" "^0.22.1" "@bazel/typescript" "^0.22.1"
"@schematics/angular" "^7.0.4" "@schematics/angular" "^7.0.4"
"@types/node" "6.0.84" "@types/node" "6.0.84"
@ -97,27 +97,27 @@
semver "5.6.0" semver "5.6.0"
symbol-observable "1.2.0" symbol-observable "1.2.0"
"@bazel/bazel-darwin_x64@0.21.0": "@bazel/bazel-darwin_x64@0.22.0":
version "0.21.0" version "0.22.0"
resolved "https://registry.yarnpkg.com/@bazel/bazel-darwin_x64/-/bazel-darwin_x64-0.21.0.tgz#db033b6880294ed274489d3bce4a36c77dbf5a7a" resolved "https://registry.yarnpkg.com/@bazel/bazel-darwin_x64/-/bazel-darwin_x64-0.22.0.tgz#a2bea5922dba9a32554a218ba4849a200115b248"
integrity sha512-9lI9SFHUm50ufJHD/5gOdJeuaI/hdGji5d0ezYWJdJK55tj4VhcatkJumjYD6yp1nPNnU038AZ7JvkPcTgt7OA== integrity sha512-LFxkyQgPATeB64z/1IvOWZhK+lc3JVHejbmdo96qB4lsoD8zselvOlgHvVXxlAjRxVZ9mlmXDvDRDyaXyyRdwA==
"@bazel/bazel-linux_x64@0.21.0": "@bazel/bazel-linux_x64@0.22.0":
version "0.21.0" version "0.22.0"
resolved "https://registry.yarnpkg.com/@bazel/bazel-linux_x64/-/bazel-linux_x64-0.21.0.tgz#d9ba05ff405c52d09878ecfb89872bda2fda418e" resolved "https://registry.yarnpkg.com/@bazel/bazel-linux_x64/-/bazel-linux_x64-0.22.0.tgz#12e5884f2a7b7f3b62afbef9f8da4de0976f3bc8"
integrity sha512-CyOblC7pMIMaXwkQazo/jz2ipmIkxngmVCTzjNKGO9GiZK71L4X/B8Simy3tFhDHZFxso2HkvJTiY1UJozFyZw== integrity sha512-xDs8cb2bbGZ9uvzYZOzCVrMBywzRhLj0J/t+py+FYZj+VO5B3wVg9eUf6nWWR0oJ2mzvToI9h31t2tNdqwy2kQ==
"@bazel/bazel-win32_x64@0.21.0": "@bazel/bazel-win32_x64@0.22.0":
version "0.21.0" version "0.22.0"
resolved "https://registry.yarnpkg.com/@bazel/bazel-win32_x64/-/bazel-win32_x64-0.21.0.tgz#f2f40f40b862f368d8596b4f69152640bd15e2ed" resolved "https://registry.yarnpkg.com/@bazel/bazel-win32_x64/-/bazel-win32_x64-0.22.0.tgz#a8a65986639583a8cc7b018e001aedfdafe41b50"
integrity sha512-ELNF4ddUCnd1Qx9359tJ5DenlVK0e5Yoe7PVv+qWNQKSCjguh8jtRy9IlzGZHjn8tFMSnTQjYYY0DgW1W1sbSA== integrity sha512-FbJaXVDoCLnpIFLnPHFkQdfriYPXfnfQNuf9EXMliERdRuoeBVbwEZfwcuArxZWNFus7bD8QiTj0XzKVWO+Wbw==
"@bazel/bazel@file:../../node_modules/@bazel/bazel": "@bazel/bazel@file:../../node_modules/@bazel/bazel":
version "0.21.0" version "0.22.0"
optionalDependencies: optionalDependencies:
"@bazel/bazel-darwin_x64" "0.21.0" "@bazel/bazel-darwin_x64" "0.22.0"
"@bazel/bazel-linux_x64" "0.21.0" "@bazel/bazel-linux_x64" "0.22.0"
"@bazel/bazel-win32_x64" "0.21.0" "@bazel/bazel-win32_x64" "0.22.0"
"@bazel/typescript@^0.22.1": "@bazel/typescript@^0.22.1":
version "0.22.1" version "0.22.1"

View File

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

View File

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