Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update bazel/create package #2665

Merged
merged 1 commit into from
May 10, 2021
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
60 changes: 46 additions & 14 deletions packages/create/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ function main(argv, error = console.error, log = console.log) {

log(`Creating Bazel workspace ${wkspName}...`);
fs.mkdirSync(wkspDir);
fs.mkdirSync(path.join(wkspDir, 'tools'));

function write(workspaceRelativePath, content) {
fs.writeFileSync(
Expand All @@ -108,7 +109,12 @@ function main(argv, error = console.error, log = console.log) {
if (args['typescript']) {
devDependencies['@bazel/typescript'] = 'latest';
devDependencies['typescript'] = 'latest';
write('tsconfig.json', '');
write('tsconfig.json', `\
{
// If building without sandboxing, we need to prevent TypeScript from descending into
// Bazel's external folder which contains third-party Bazel dependencies.
"exclude": ["external/*"]
}`);
rootBuildContent += '# Allow any ts_library rules in this workspace to reference the config\n' +
'# Note: if you move the tsconfig.json file to a subdirectory, you can add an alias() here instead\n' +
'# so that ts_library rules still use it by default.\n' +
Expand All @@ -133,13 +139,37 @@ yarn_install(
`# The npm_install rule runs yarn anytime the package.json or package-lock.json file changes.
# It also extracts any Bazel rules distributed in an npm package.
load("@build_bazel_rules_nodejs//:index.bzl", "npm_install")

npm_install(
# Name this npm so that Bazel Label references look like @npm//package
name = "npm",
package_json = "//:package.json",
package_lock_json = "//:package-lock.json",
)`;

let bazelDepsContent = `# Third-party dependencies fetched by Bazel
# Unlike WORKSPACE, the content of this file is unordered.
# We keep them separate to make the WORKSPACE file more maintainable.

# Install the nodejs "bootstrap" package
# This provides the basic tools for running and packaging nodejs programs in Bazel
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
def fetch_dependencies():
http_archive(
name = "build_bazel_rules_nodejs",
sha256 = "65067dcad93a61deb593be7d3d9a32a4577d09665536d8da536d731da5cd15e2",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/3.4.2/rules_nodejs-3.4.2.tar.gz"],
)

# rules_nodejs doesn't depend on skylib, but it's a useful dependency anyway.
http_archive(
name = "bazel_skylib",
urls = [
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.0.3/bazel-skylib-1.0.3.tar.gz",
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.0.3/bazel-skylib-1.0.3.tar.gz",
],
sha256 = "1c531376ac7e5a180e0237938a2536de0c54d93f5c278634818e0efc952dd56c",
)`
let workspaceContent = `# Bazel workspace created by @bazel/create 0.0.0-PLACEHOLDER

# Declares that this directory is the root of a Bazel workspace.
Expand All @@ -152,22 +182,23 @@ workspace(
managed_directories = {"@npm": ["node_modules"]},
)

# Install the nodejs "bootstrap" package
# This provides the basic tools for running and packaging nodejs programs in Bazel
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "build_bazel_rules_nodejs",
sha256 = "65067dcad93a61deb593be7d3d9a32a4577d09665536d8da536d731da5cd15e2",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/3.4.2/rules_nodejs-3.4.2.tar.gz"],
)
load("//tools:bazel_deps.bzl", "fetch_dependencies")

fetch_dependencies()

${pkgMgr === 'yarn' ? yarnInstallCmd : npmInstallCmd}`;

write('WORKSPACE.bazel', workspaceContent);
write('.bazelignore', `node_modules
write('tools/BUILD.bazel', '# Currently there are no targets in this Bazel package')
write('tools/bazel_deps.bzl', bazelDepsContent);
// Don't name it WORKSPACE.bazel since there's a bug with managed_directories
Copy link

Choose a reason for hiding this comment

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

@alexeagle could you please share more details about this bug? Does it mean that "workspace" file should be used always without .bazel extension, or it affects only bazel/create package?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@gregmagolan is tracking down the bug which hasn't been filed upstream yet. It affects everyone, not just this bazel/create package.

write('WORKSPACE', workspaceContent);
write('.bazelignore', `\
# NB: semantics here are not the same as .gitignore
# see https://github.com/bazelbuild/bazel/issues/8106
# For example, every nested node_modules directory needs to be listed here.
node_modules
dist
bazel-out
`);
bazel-out`);
write(
'package.json',
JSON.stringify(
Expand All @@ -182,7 +213,8 @@ bazel-out
}
},
null, 4));
write('.gitignore', `
write('.gitignore', `\
.bazelrc.user
dist
bazel-out
node_modules`);
Expand Down
6 changes: 3 additions & 3 deletions packages/create/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,23 @@ if (projFiles.indexOf('.bazelrc') < 0) {
if (projFiles.indexOf('.bazelversion') < 0) {
fail('project should have .bazelversion');
}
let wkspContent = read('some_project/WORKSPACE.bazel');
let wkspContent = read('some_project/WORKSPACE');
if (wkspContent.indexOf('npm_install(') < 0) {
fail('should use npm by default');
}
// TODO: run bazel in the new directory to verify a build works

exitCode = main(['configure_pkgMgr', '--packageManager=yarn'], captureError);
if (exitCode != 0) fail('should be success');
wkspContent = read('configure_pkgMgr/WORKSPACE.bazel');
wkspContent = read('configure_pkgMgr/WORKSPACE');
if (wkspContent.indexOf('yarn_install(') < 0) {
fail('should use yarn when requested');
}

process.env['_'] = '/usr/bin/yarn';
exitCode = main(['default_to_yarn']);
if (exitCode != 0) fail('should be success');
wkspContent = read('default_to_yarn/WORKSPACE.bazel');
wkspContent = read('default_to_yarn/WORKSPACE');
if (wkspContent.indexOf('yarn_install(') < 0) {
fail('should use yarn by default');
}
Expand Down