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

Support for entry points relative to current BUILD file directory #32

Closed
rzhw opened this issue Oct 5, 2017 · 5 comments · Fixed by #777
Closed

Support for entry points relative to current BUILD file directory #32

rzhw opened this issue Oct 5, 2017 · 5 comments · Fixed by #777
Assignees
Milestone

Comments

@rzhw
Copy link
Contributor

rzhw commented Oct 5, 2017

Currently, users provide the full path to a local js file to use it as an entry point, including the workspace name. It's a bit of extra typing, but it's workable.

This gets a bit more cumbersome for nodejs_binary using entry points that aren't at fixed locations e.g. from other rules in macros. (FWIW this isn't an issue for rules e.g. ctx.workspace_name + "/" + ctx.file.foo.path, but it's unrelated because rules can't call other rules.)

Below is an example: we have an intermediary rule which outputs a js file, and we want the nodejs_binary to use it as an entry point. But the macro doesn't know the path to the js file, so we have to get the user to specify the path.

def foo_rule(
        name,
        # "workspace_name/path/to/BUILD/directory/"
        current_path,
        visibility = None):

    # Intermediary rule outputs "{name}.js".
    intermediary_name = "%s.intermediary" % name
    intermediary_rule(
        name = intermediary_name,
    )

    # Nodejs rule combines current path with known output name
    nodejs_binary(
        name = name,
        data = [
            "@//:node_modules",
            intermediary_name,
        ],
        entry_point = "%s%s.js" % (current_path, intermediary_name),
        visibility = visibility,
    )

Proposal

Add the current path to the node module resolver's list of resolve paths.

The above entry point can be changed to a relative path:

    nodejs_binary(
        # ...
        entry_point = "%s.intermediary.js" % name,

Caveats

Files supplied in data would clash with node_modules. Convoluted example:

    nodejs_binary(
        # ...
        data = [
            "@//:node_modules",
            "some_node_module/foo.js",
        ],
        entry_point = "example.js",
    )
const foo = require('some_node_module/foo.js'); // Which one?

Alternatives considered

Change entry_point to be attr.label instead of attr.string

Ruled out because this would break references to node_modules.

Split entry_point into two attributes, one attr.label for local references, one attr.string for node_modules references

Not as straightforward for users.

Split nodejs_binary into two rules, one for local entry points, one for node_modules entry points

Not as straightforward for users.

@rzhw rzhw changed the title Running local js file requiring full path including workspace name Running local sources requiring full path including workspace name Oct 5, 2017
@rzhw rzhw changed the title Running local sources requiring full path including workspace name Local sources requiring full path including workspace name Oct 5, 2017
@rzhw rzhw changed the title Local sources requiring full path including workspace name Support for entry points relative to current BUILD file directory Oct 11, 2017
@alexeagle
Copy link
Collaborator

Sorry it took me so long to reply, and thanks for sending a PR for this.

I'm concerned about the namespace collisions, as you point out. I think the entry_point needs some specific syntactic hint that we want to load from the package. This could be as simple as starting with ./

Note that any build file can use the https://docs.bazel.build/versions/master/skylark/lib/native.html#package_name and repository_name functions to do the same thing as ./, but it's tricky because repository_name could be empty or it could be @name, which needs a bit of logic to convert to a path.

You also suggested allowing a label in the entry_point - this is already supported by bazel with special $(location :label) syntax. That's the most explicit way to do this, so I gave it a try:
#42

what do you think about that approach?

alexeagle added a commit to alexeagle/rules_nodejs that referenced this issue Oct 31, 2017
alexeagle added a commit to alexeagle/rules_nodejs that referenced this issue Oct 31, 2017
alexeagle added a commit to alexeagle/rules_nodejs that referenced this issue Oct 31, 2017
alexeagle added a commit to alexeagle/rules_nodejs that referenced this issue Oct 31, 2017
alexeagle added a commit to alexeagle/rules_nodejs that referenced this issue Oct 31, 2017
@rzhw
Copy link
Contributor Author

rzhw commented Nov 1, 2017

Thanks for the response! Apologies about the equally late reply 😛

Great points, and I agree the namespace collision is a major downside of this suggestion. Also wasn't aware of those native.* functions, cool! Shame they can't really be used, so the $(location :label) syntax SGTM; I'll reply to the linked PR.

alexeagle added a commit to alexeagle/rules_nodejs that referenced this issue Nov 3, 2017
alexeagle added a commit to alexeagle/rules_nodejs that referenced this issue Nov 3, 2017
@nictuku
Copy link

nictuku commented Feb 11, 2018

Hi. Right now, folks I talked to are having trouble figuring out how to use entry_point, probably because the instructions are unclear and there is no support for $(location).

@alexeagle seems to be working on support $(location :file.js) for the entry_point. Any ETA for when that's going to land? Thanks!

rzhw pushed a commit to rzhw/rules_nodejs that referenced this issue Feb 12, 2018
rzhw pushed a commit to rzhw/rules_nodejs that referenced this issue Feb 12, 2018
alexeagle added a commit to alexeagle/rules_nodejs that referenced this issue May 14, 2018
BREAKING CHANGE:
This forces us to use workspace-relative paths since the rootpath helper returns this format.
Users are now exposed to the external path segment to reference other workspaces.
See discussion: bazel-contrib#84

Fixes bazel-contrib#32
@alexeagle
Copy link
Collaborator

Instead of using location or rootpath helpers, we should change entry_point to be a label.
#42 (comment)

@alexeagle alexeagle added this to the Beta milestone Nov 30, 2018
@manekinekko manekinekko self-assigned this Dec 18, 2018
gregmagolan pushed a commit to gregmagolan/rules_nodejs that referenced this issue Mar 21, 2019
gregmagolan pushed a commit to gregmagolan/rules_nodejs that referenced this issue Mar 26, 2019
gregmagolan pushed a commit to gregmagolan/rules_nodejs that referenced this issue Mar 26, 2019
alexeagle pushed a commit to alexeagle/rules_nodejs that referenced this issue Oct 17, 2020
Explicitly set/override the users declarationDir setting to
suppress *.d.ts files from being emitted in a non-predicatable
fashion.

Fixes bazel-contrib#32
Closes bazel-contrib#33

PiperOrigin-RevId: 168608369
alexeagle pushed a commit to alexeagle/rules_nodejs that referenced this issue Oct 18, 2020
Explicitly set/override the users declarationDir setting to
suppress *.d.ts files from being emitted in a non-predicatable
fashion.

Fixes bazel-contrib#32
Closes bazel-contrib#33

PiperOrigin-RevId: 168608369
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment