Skip to content

Commit

Permalink
test: add npm_translate_lock_replace_packages (#1888)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard authored Aug 14, 2024
1 parent c996609 commit 90b64b6
Show file tree
Hide file tree
Showing 28 changed files with 394 additions and 354 deletions.
13 changes: 13 additions & 0 deletions .aspect/workflows/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ workspaces:
without: true
- delivery:
without: true
e2e/npm_translate_lock_replace_packages:
icon: npm
tasks:
- test:
queue: aspect-medium
- format:
without: true
- buildifier:
without: true
- configure:
without: true
- delivery:
without: true
e2e/npm_translate_lock_empty:
icon: npm
tasks:
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,14 @@ jobs:
- os: windows
# Exclude MacOS by default, will opt-in to includes
- os: macos
# Don't run bzlmod smoke test under workspace
- bzlmod: 0
folder: e2e/bzlmod
# Don't run replace_packages test with bzlmod+Bazel 6 due to use of Bazel 7 bzlmod features
- bazel-version:
major: 6
- bzlmod: 1
folder: e2e/npm_translate_lock_replace_packages
# Don't run RBE tests with Bazel 6 to reduce the size of the test matrix
- bazel-version:
major: 6
Expand Down
4 changes: 0 additions & 4 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -206,17 +206,13 @@ npm.npm_translate_lock(
# other direct dependencies in the `examples/npm_deps/package.json`.
"[email protected]": ["examples/npm_deps"],
},
replace_packages = {
"[email protected]": "@chalk_501//:pkg",
},
update_pnpm_lock = True,
verify_node_modules_ignored = "//:.bazelignore",
verify_patches = "//examples/npm_deps/patches:patches",
)
use_repo(
npm,
"npm",
"npm__chalk__5.0.1__links",
"npm__fsevents__2.3.2__links",
"npm__rollup__2.70.2",
"npm__rollup__2.70.2__links",
Expand Down
3 changes: 0 additions & 3 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,6 @@ npm_translate_lock(
# other direct dependencies in the `examples/npm_deps/package.json`.
"[email protected]": ["examples/npm_deps"],
},
replace_packages = {
"[email protected]": "@chalk_501//:pkg",
},
update_pnpm_lock = True,
verify_node_modules_ignored = "//:.bazelignore",
verify_patches = "//examples/npm_deps/patches:patches",
Expand Down
7 changes: 0 additions & 7 deletions WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ fetch_shfmt()
# dev dependency
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "chalk_501",
build_file = "//npm/private/test:vendored/chalk-5.0.1.BUILD",
integrity = "sha256-/nD5GSp77HDNFDwIL68S5PbS+8gefWkube2iIr80/x4=",
url = "https://registry.npmjs.org/chalk/-/chalk-5.0.1.tgz",
)

http_archive(
name = "com_grail_bazel_toolchain",
sha256 = "a9fc7cf01d0ea0a935bd9e3674dd3103766db77dfc6aafcb447a7ddd6ca24a78",
Expand Down
1 change: 1 addition & 0 deletions e2e/npm_translate_lock_replace_packages/.bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
15 changes: 15 additions & 0 deletions e2e/npm_translate_lock_replace_packages/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Import Aspect bazelrc presets
try-import %workspace%/../../.aspect/bazelrc/bazel7.bazelrc
import %workspace%/../../.aspect/bazelrc/convenience.bazelrc
import %workspace%/../../.aspect/bazelrc/correctness.bazelrc
import %workspace%/../../.aspect/bazelrc/debug.bazelrc
import %workspace%/../../.aspect/bazelrc/javascript.bazelrc
import %workspace%/../../.aspect/bazelrc/performance.bazelrc

### YOUR PROJECT SPECIFIC OPTIONS GO HERE ###

# Load any settings & overrides specific to the current user from `.aspect/bazelrc/user.bazelrc`.
# This file should appear in `.gitignore` so that settings are not shared with team members. This
# should be last statement in this config so the user configuration is able to overwrite flags from
# this file. See https://bazel.build/configure/best-practices#bazelrc-file.
try-import %workspace%/../../.aspect/bazelrc/user.bazelrc
1 change: 1 addition & 0 deletions e2e/npm_translate_lock_replace_packages/.bazelversion
4 changes: 4 additions & 0 deletions e2e/npm_translate_lock_replace_packages/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Disabling pnpm [hoisting](https://pnpm.io/npmrc#hoist) by setting `hoist=false` is recommended on
# projects using rules_js so that pnpm outside of Bazel lays out a node_modules tree similar to what
# rules_js lays out under Bazel (without a hidden node_modules/.pnpm/node_modules)
hoist=false
68 changes: 68 additions & 0 deletions e2e/npm_translate_lock_replace_packages/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")
load("@aspect_rules_js//js:defs.bzl", "js_test")
load("@aspect_rules_js//npm:defs.bzl", "npm_package")
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@npm//:defs.bzl", "npm_link_all_packages")

npm_link_all_packages(name = "node_modules")

js_test(
name = "test",
data = [
"package.json",
":node_modules",
],
entry_point = "main.js",
)

npm_package(
name = "npm-pkg",
srcs = [
"main.js",
"package.json",
],
)

build_test(
name = "node_modules_test",
targets = [
":node_modules",
":npm-pkg",
],
)

# Strip the system-dependent 'system_tar' attribute from the generated repositories.bzl
genrule(
name = "npm_translate_lock_wksp-repositories",
srcs = ["@npm//:repositories.bzl"],
outs = ["wksp_repos_stripped.bzl"],
cmd = 'sed "s/system_tar = .*,/system_tar = \\"xxx\\",/" "$<" > "$@"',
target_compatible_with = select({
"@aspect_bazel_lib//lib:bzlmod": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
)

write_source_files(
name = "write_npm_translate_lock_wksp",
files = {
"snapshots/wksp/npm_defs.bzl": "@npm//:defs.bzl",
"snapshots/wksp/repositories.bzl": ":npm_translate_lock_wksp-repositories",
},
target_compatible_with = select({
"@aspect_bazel_lib//lib:bzlmod": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
)

write_source_files(
name = "write_npm_translate_lock_bzlmod",
files = {
# NOTE: @npm//:repositories.bzl not generated for bzlmod
"snapshots/bzlmod/npm_defs.bzl": "@npm//:defs.bzl",
},
target_compatible_with = select({
"@aspect_bazel_lib//lib:bzlmod": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
)
42 changes: 42 additions & 0 deletions e2e/npm_translate_lock_replace_packages/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module(
name = "e2e_npm_translate_lock_replace_packages",
version = "0.0.0",
compatibility_level = 1,
)

bazel_dep(name = "platforms", version = "0.0.5")
bazel_dep(name = "aspect_rules_js", version = "0.0.0")
bazel_dep(name = "aspect_bazel_lib", version = "2.7.7")
bazel_dep(name = "bazel_skylib", version = "1.5.0")

local_path_override(
module_name = "aspect_rules_js",
path = "../..",
)

npm = use_extension(
"@aspect_rules_js//npm:extensions.bzl",
"npm",
dev_dependency = True,
)
npm.npm_translate_lock(
name = "npm",
npmrc = "//:.npmrc",
pnpm_lock = "//:pnpm-lock.yaml",
replace_packages = {
"[email protected]": "@chalk_501//:pkg",
},
verify_node_modules_ignored = "//:.bazelignore",
)

http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "chalk_501",
build_file = "//:chalk-5.0.1.BUILD",
dev_dependency = True,
integrity = "sha256-/nD5GSp77HDNFDwIL68S5PbS+8gefWkube2iIr80/x4=",
url = "https://registry.npmjs.org/chalk/-/chalk-5.0.1.tgz",
)

use_repo(npm, "npm")
37 changes: 37 additions & 0 deletions e2e/npm_translate_lock_replace_packages/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
local_repository(
name = "aspect_rules_js",
path = "../..",
)

load("@aspect_rules_js//js:repositories.bzl", "rules_js_dependencies")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

rules_js_dependencies()

load("@aspect_rules_js//js:toolchains.bzl", "DEFAULT_NODE_VERSION", "rules_js_register_toolchains")

rules_js_register_toolchains(node_version = DEFAULT_NODE_VERSION)

load("@aspect_rules_js//npm:repositories.bzl", "npm_translate_lock")

npm_translate_lock(
name = "npm",
data = ["//:package.json"],
npmrc = "//:.npmrc",
pnpm_lock = "//:pnpm-lock.yaml",
replace_packages = {
"[email protected]": "@chalk_501//:pkg",
},
verify_node_modules_ignored = "//:.bazelignore",
)

load("@npm//:repositories.bzl", "npm_repositories")

npm_repositories()

http_archive(
name = "chalk_501",
build_file = "//:chalk-5.0.1.BUILD",
integrity = "sha256-/nD5GSp77HDNFDwIL68S5PbS+8gefWkube2iIr80/x4=",
url = "https://registry.npmjs.org/chalk/-/chalk-5.0.1.tgz",
)
2 changes: 2 additions & 0 deletions e2e/npm_translate_lock_replace_packages/WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# The presence of this file causes WORKSPACE to be ignored when bzlmod is enabled.
# See https://docs.google.com/document/d/1JtXIVnXyFZ4bmbiBCr5gsTH4-opZAFf5DMMb-54kES0/edit#heading=h.y054fjub9max
File renamed without changes.
21 changes: 21 additions & 0 deletions e2e/npm_translate_lock_replace_packages/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import chalk from 'chalk'
import { readFileSync } from 'fs'

const packageJsonDep = JSON.parse(readFileSync('./package.json', 'utf-8'))
.dependencies['chalk']
if (packageJsonDep !== '5.3.0') {
throw new Error(
`Expected chalk version 5.3.0 declared in package.json, but got ${pkgDep}`
)
}

const actualDep = JSON.parse(
readFileSync('./node_modules/chalk/package.json', 'utf-8')
).version
if (actualDep !== '5.0.1') {
throw new Error(
`Expected chalk to be replaced with version 5.0.1, but got ${actualDep}`
)
}

console.log(chalk.blue(`Hello world! The meaning of life is... 42`))
10 changes: 10 additions & 0 deletions e2e/npm_translate_lock_replace_packages/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"private": true,
"type": "module",
"dependencies": {
"chalk": "5.3.0"
},
"pnpm": {
"onlyBuiltDependencies": []
}
}
23 changes: 23 additions & 0 deletions e2e/npm_translate_lock_replace_packages/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions e2e/npm_translate_lock_replace_packages/pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
packages:
- '.'
Empty file.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
Loading

0 comments on commit 90b64b6

Please sign in to comment.