Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ npm/private/test/package.json=322962683
npm/private/test/vendored/is-odd/package.json=1041695223
npm/private/test/vendored/lodash-4.17.21.tgz=-1206623349
npm/private/test/vendored/semver-max/package.json=578664053
package.json=1068891966
pnpm-lock.yaml=1390701932
package.json=234760749
pnpm-lock.yaml=43430792
pnpm-workspace.yaml=-2039776064
6 changes: 6 additions & 0 deletions js/private/node-patches/fs.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const util = require("util");
// es modules
const fs = require('node:fs');
const url = require('node:url');
const esmModule = require('node:module');
const HOP_NON_LINK = Symbol.for('HOP NON LINK');
const HOP_NOT_FOUND = Symbol.for('HOP NOT FOUND');
const PATCHED_FS_METHODS = [
Expand Down Expand Up @@ -747,12 +748,17 @@ function patcher(roots) {
}
}
}
// Sync the esm modules to use the now patched fs cjs module.
// See: https://nodejs.org/api/esm.html#builtin-modules
esmModule.syncBuiltinESMExports();
return function revertPatch() {
Object.assign(fs, fs._unpatched);
delete fs._unpatched;
if (unpatchPromises) {
unpatchPromises();
}
// Re-sync the esm modules to revert to the unpatched module.
esmModule.syncBuiltinESMExports();
};
}
// =========================================================================
Expand Down
5 changes: 0 additions & 5 deletions js/private/node-patches/register.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,11 @@ if (
JS_BINARY__PATCH_NODE_FS != '0' &&
JS_BINARY__FS_PATCH_ROOTS
) {
const module = require('node:module')
const roots = JS_BINARY__FS_PATCH_ROOTS.split(':')
if (JS_BINARY__LOG_DEBUG) {
console.error(
`DEBUG: ${JS_BINARY__LOG_PREFIX}: node fs patches will be applied with roots: ${roots}`
)
}
patchfs(roots)

// Sync the esm modules to use the now patched fs cjs module.
// See: https://nodejs.org/api/esm.html#builtin-modules
module.syncBuiltinESMExports()
}
8 changes: 8 additions & 0 deletions js/private/node-patches/src/fs.cts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Dirent = any
// es modules
const fs = require('node:fs') as any
const url = require('node:url') as typeof UrlType
const esmModule = require('node:module')

const HOP_NON_LINK = Symbol.for('HOP NON LINK')
const HOP_NOT_FOUND = Symbol.for('HOP NOT FOUND')
Expand Down Expand Up @@ -857,13 +858,20 @@ export function patcher(roots: string[]): () => void {
}
}

// Sync the esm modules to use the now patched fs cjs module.
// See: https://nodejs.org/api/esm.html#builtin-modules
esmModule.syncBuiltinESMExports()

return function revertPatch() {
Object.assign(fs, fs._unpatched)
delete fs._unpatched

if (unpatchPromises) {
unpatchPromises()
}

// Re-sync the esm modules to revert to the unpatched module.
esmModule.syncBuiltinESMExports()
}
}

Expand Down
2 changes: 1 addition & 1 deletion js/private/test/image/checksum_test.expected
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
a4232a52470e03fc555dfc1bd5a6e83090a71bccad58eb45fbc55f780f84df1e js/private/test/image/cksum_node
e0580cd2b3a537cb7147139b7fe79ac48c1200fc2cbf69881a9ee9f68aeaeae4 js/private/test/image/cksum_node
52836a988c8ac815b4a3b70fa3a3acec67b851699fa989694cef4cc1fa53de96 js/private/test/image/cksum_package_store_3p
642b308a0561fb51dfd96d08d74a4ec419c9d2ca501cfa1002a49c8e25fbe4c2 js/private/test/image/cksum_package_store_1p
5d45f32dacf0b83e26c33d4e1017c694e79eaff29b8ecc336f9ea8fdee870d45 js/private/test/image/cksum_node_modules
4 changes: 2 additions & 2 deletions js/private/test/image/custom_layers_nomatch_test_node.listing
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/
-r-xr-xr-x 0 0 0 34161 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/fs.cjs
-r-xr-xr-x 0 0 0 1664 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/register.cjs
-r-xr-xr-x 0 0 0 34481 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/fs.cjs
-r-xr-xr-x 0 0 0 1460 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/register.cjs
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/bin/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/bin/nodejs/
Expand Down
4 changes: 2 additions & 2 deletions js/private/test/image/custom_owner_test_node.listing
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ drwxr-xr-x 0 100 0 0 Jan 1 1970 ./js/private/test/image/bin.runf
drwxr-xr-x 0 100 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/
drwxr-xr-x 0 100 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/
drwxr-xr-x 0 100 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/node-patches/
-r-xr-xr-x 0 100 0 34161 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/node-patches/fs.cjs
-r-xr-xr-x 0 100 0 1664 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/node-patches/register.cjs
-r-xr-xr-x 0 100 0 34481 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/node-patches/fs.cjs
-r-xr-xr-x 0 100 0 1460 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/node-patches/register.cjs
drwxr-xr-x 0 100 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/
drwxr-xr-x 0 100 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/bin/
drwxr-xr-x 0 100 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/bin/nodejs/
Expand Down
4 changes: 2 additions & 2 deletions js/private/test/image/default_test_node.listing
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./js/private/test/image/bin.runf
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/node-patches/
-r-xr-xr-x 0 0 0 34161 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/node-patches/fs.cjs
-r-xr-xr-x 0 0 0 1664 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/node-patches/register.cjs
-r-xr-xr-x 0 0 0 34481 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/node-patches/fs.cjs
-r-xr-xr-x 0 0 0 1460 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/node-patches/register.cjs
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/bin/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/bin/nodejs/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/non_
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/private/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/private/node-patches/
-r-xr-xr-x 0 0 0 34161 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/private/node-patches/fs.cjs
-r-xr-xr-x 0 0 0 34481 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/private/node-patches/fs.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/non_
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/private/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/private/node-patches/
-r-xr-xr-x 0 0 0 1664 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/private/node-patches/register.cjs
-r-xr-xr-x 0 0 0 1460 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/private/node-patches/register.cjs
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/rules_nodejs~~node~nodejs_linux_amd64/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/rules_nodejs~~node~nodejs_linux_amd64/bin/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/rules_nodejs~~node~nodejs_linux_amd64/bin/nodejs/
Expand Down
4 changes: 2 additions & 2 deletions js/private/test/image/regex_edge_cases_test_node.listing
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/
-r-xr-xr-x 0 0 0 34161 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/fs.cjs
-r-xr-xr-x 0 0 0 1664 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/register.cjs
-r-xr-xr-x 0 0 0 34481 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/fs.cjs
-r-xr-xr-x 0 0 0 1460 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/register.cjs
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/bin/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/bin/nodejs/
Expand Down
97 changes: 74 additions & 23 deletions js/private/test/node-patches/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_to_bin")
load("@npm//:@babel/cli/package_json.bzl", babel_bin = "bin")
load("//js:defs.bzl", "js_test")

TESTS = [
"escape.js",
"lstat.js",
"opendir.js",
"readdir.js",
"readlink.js",
"realpath.js",
]

MJS_TESTS = [
"lstat.mjs",
"opendir.mjs",
"readdir.mjs",
"readlink.mjs",
"realpath.mjs",
]

CJS_TESTS = [
t.replace(".mjs", ".cjs")
for t in MJS_TESTS
]

# Multiple node toolchains for testing across versions
Expand Down Expand Up @@ -36,29 +45,71 @@ TOOLCHAINS_VERSIONS = [
name = "copy_entry_{}".format(t),
srcs = [t],
)
for t in TESTS
for t in TESTS + MJS_TESTS
]

babel_bin.babel(
name = "babel_mjs2js",
srcs = MJS_TESTS + [
"babel.config.js",
"//:node_modules/@babel/plugin-transform-modules-commonjs",
],
outs = [t.replace(".mjs", ".cjs") for t in MJS_TESTS],
args = [
"--extensions",
".mjs",
"--out-file-extension",
".cjs",
"--out-dir",
".",
] + MJS_TESTS,
chdir = package_name(), # to automatically pickup babel.config.js
)

# Basic tests
[
[
js_test(
name = "{}_{}_test".format(
t.replace(".js", ""),
toolchain_name,
),
data = [
"//:node_modules/inline-fixtures",
"//js/private/node-patches/src:compile",
],
entry_point = "copy_entry_{}".format(t),
node_toolchain = toolchain,
patch_node_fs = False,
# Without node patches on for these tests, the program is going to escape the sandbox if it
# is on since the fs patches are not on for the tests as they are the code under test
tags = ["no-sandbox"],
)
for t in TESTS
# The primary tests, both .js and .mjs
[
js_test(
name = "{}_{}_test".format(
t.replace(".mjs", "").replace(".js", ""),
toolchain_name,
),
data = [
"//:node_modules/inline-fixtures",
"//js/private/node-patches/src:compile",
],
entry_point = "copy_entry_{}".format(t),
node_toolchain = toolchain,
patch_node_fs = False,
# Without node patches on for these tests, the program is going to escape the sandbox if it
# is on since the fs patches are not on for the tests as they are the code under test
tags = ["no-sandbox"],
)
for t in TESTS + MJS_TESTS
],

# The .cjs tests converted from .mjs
[
js_test(
name = "{}_{}_cjs_test".format(
t.replace(".cjs", ""),
toolchain_name,
),
data = [
"//:node_modules/inline-fixtures",
"//js/private/node-patches/src:compile",
],
entry_point = t,
node_toolchain = toolchain,
patch_node_fs = False,
# Without node patches on for these tests, the program is going to escape the sandbox if it
# is on since the fs patches are not on for the tests as they are the code under test
tags = ["no-sandbox"],
)
for t in CJS_TESTS
],
]
for toolchain_name, toolchain in zip(
TOOLCHAINS_NAMES,
Expand Down
10 changes: 10 additions & 0 deletions js/private/test/node-patches/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
plugins: [
[
'@babel/plugin-transform-modules-commonjs',
{
importInterop: 'none',
},
],
],
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const assert = require('assert')
const fs = require('fs')
const withFixtures = require('inline-fixtures').withFixtures
const path = require('path')
const util = require('util')
import * as assert from 'node:assert'
import * as fs from 'node:fs'
import { withFixtures } from 'inline-fixtures'
import * as path from 'node:path'
import * as util from 'node:util'

const patcher = require('../../node-patches/src/fs.cjs').patcher
import { patcher } from '../../node-patches/src/fs.cjs'

// We don't want to bring jest into this repo so we just fake the describe and it functions here
async function describe(_, fn) {
Expand Down Expand Up @@ -197,7 +197,7 @@ describe('testing lstat', async () => {
brokenLinkPath
)

stat = await fs.promises.lstat(brokenLinkPath)
const stat = await fs.promises.lstat(brokenLinkPath)
assert.ok(
stat.isSymbolicLink(),
'if a symlink is broken but not escaping return it as a link.'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const assert = require('assert')
const fs = require('fs')
const withFixtures = require('inline-fixtures').withFixtures
const path = require('path')
const util = require('util')
import * as assert from 'node:assert'
import * as fs from 'node:fs'
import { withFixtures } from 'inline-fixtures'
import * as path from 'node:path'
import * as util from 'node:util'

const patcher = require('../../node-patches/src/fs.cjs').patcher
import { patcher } from '../../node-patches/src/fs.cjs'

// We don't want to bring jest into this repo so we just fake the describe and it functions here
async function describe(_, fn) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const assert = require('assert')
const fs = require('fs')
const withFixtures = require('inline-fixtures').withFixtures
const path = require('path')
const util = require('util')
import * as assert from 'node:assert'
import * as fs from 'node:fs'
import { withFixtures } from 'inline-fixtures'
import * as path from 'node:path'
import * as util from 'node:util'

const patcher = require('../../node-patches/src/fs.cjs').patcher
import { patcher } from '../../node-patches/src/fs.cjs'

// We don't want to bring jest into this repo so we just fake the describe and it functions here
async function describe(_, fn) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const assert = require('assert')
const fs = require('fs')
const withFixtures = require('inline-fixtures').withFixtures
const path = require('path')
const util = require('util')
import * as assert from 'node:assert'
import * as fs from 'node:fs'
import { withFixtures } from 'inline-fixtures'
import * as path from 'node:path'
import * as util from 'node:util'

const patcher = require('../../node-patches/src/fs.cjs').patcher
import { patcher } from '../../node-patches/src/fs.cjs'

// We don't want to bring jest into this repo so we just fake the describe and it functions here
async function describe(_, fn) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const assert = require('assert')
const fs = require('fs')
const withFixtures = require('inline-fixtures').withFixtures
const path = require('path')
const util = require('util')
import * as assert from 'node:assert'
import * as fs from 'node:fs'
import { withFixtures } from 'inline-fixtures'
import * as path from 'node:path'
import * as util from 'node:util'

const patcher = require('../../node-patches/src/fs.cjs').patcher
import { patcher } from '../../node-patches/src/fs.cjs'

// We don't want to bring jest into this repo so we just fake the describe and it functions here
async function describe(_, fn) {
Expand Down
Loading
Loading