aspect-build / rules_js

High-performance Bazel rules for running Node.js tools and building JavaScript projects
https://docs.aspect.build/rules/aspect_rules_js
Apache License 2.0
300 stars 102 forks source link

[Bug]: patch_node_fs = True cause next.js standalone build not properly symlink node modules #1033

Open zfy0701 opened 1 year ago

zfy0701 commented 1 year ago

What happened?

when building with next, node module inside standalone will be symlinks, but the files generated by bazel build will have symlink to some absolute path, make the following packing process very hard, make patch_node_fs = False seems works

Version

Development (host) and target OS/architectures:

Output of bazel --version:

Version of the Aspect rules, or other relevant rules from your WORKSPACE or MODULE.bazel file:

Language(s) and/or frameworks involved:

How to reproduce

No response

Any other information?

No response

christian-roggia commented 1 year ago

Do you mind sharing a minimal setup or repository for building with standalone? Thanks!

alexeagle commented 1 year ago

+1 - there's not enough info in the original post for us to debug this.

How to reproduce No response

gregjacobs commented 1 year ago

Oof, so I think I'm running into this issue as well, except using webpack_devserver instead of Next.js.

It seems that some of the modules are being resolved in the js_run_devserver sandbox (which symlinks to the runfiles' node_modules), but other modules are being resolved to the root node_modules (seemingly transitive dependencies).

node_modules path 1 (root):

<execution_root>/<project>/bazel-out/darwin-opt/bin/node_modules/.aspect_rules_js

node_modules path 2 (runfiles):

<execution_root>/<project>/bazel-out/darwin-opt/bin/path/to/server.sh.runfiles/node_modules/.aspect_rules_js

And sadly, Webpack is resolving modules like 'react' and other 1st-party dependencies in both trees (which immediately causes React hook errors).

I'm trying to create a minimal reproduction that I can send in but having a bit of trouble reproducing in a smaller context. (The project I'm working on is quite large so I'm trying to figure out what exactly triggers the problem)

zfy0701 commented 1 year ago

+1 - there's not enough info in the original post for us to debug this.

How to reproduce No response

@alexeagle will get back to you with example

dmeijboom commented 6 months ago

We're having the same issue, error:

INFO: Analyzed target //frontend:frontend (597 packages loaded, 9081 targets configured).
ERROR: /[redacted]/frontend/BUILD:21:14: Error while validating output TreeArtifact File:[[<execution_root>]bazel-out/darwin_arm64-fastbuild/bin]frontend/.next : Failed to resolve relative path standalone/node_modules/next inside TreeArtifact /private/var/tmp/_bazel_[redacted]/b6b87a026ed65b9d149b63a85b35b10b/execroot/_main/bazel-out/darwin_arm64-fastbuild/bin/frontend/.next. The associated file is either missing or is an invalid symlink.
ERROR: /[redacted]/frontend/BUILD:21:14: JsRunBinary frontend/.next failed: not all outputs were created or valid
Target //frontend:frontend failed to build

We using NextJS 14.1.0 with output set to standalone in next.config.js. Our build file looks like this:

js_run_binary(
    name = "frontend",
    srcs = [
         // sources
    ],
    args = [
        "build",
        "--no-lint",
    ],
    chdir = package_name(),
    out_dirs = [".next"],
    tool = ":next_js_binary",
    visibility = ["//visibility:public"],
)

The issue seems to be that the NextJS project works as intended but it creates .next/standalone which has it's own node_modules folder where the symlinks do not resolve.