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

cargo:rustc-link-search becomes invalid when you move a Cargo project #4053

Closed
japaric opened this issue May 15, 2017 · 2 comments · Fixed by #4818
Closed

cargo:rustc-link-search becomes invalid when you move a Cargo project #4053

japaric opened this issue May 15, 2017 · 2 comments · Fixed by #4818
Labels
A-build-scripts Area: build.rs scripts A-linkage Area: linker issues, dylib, cdylib, shared libraries, so C-bug Category: bug

Comments

@japaric
Copy link
Member

japaric commented May 15, 2017

STR

# create a dependency that uses cargo:rustc-link-search
$ cargo new --lib foo && cd $_
$ edit build.rs && cat $_
use std::{env, fs};
use std::path::PathBuf;

fn main() {
    let src_dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
    let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());

    fs::copy(src_dir.join("link.x"), out_dir.join("link.x")).unwrap();
    println!("cargo:rustc-link-search={}", out_dir.display());
    println!("cargo:rerun-if-changed=build.rs");
    println!("cargo:rerun-if-changed=link.x");
}
# create an empty linker script
$ touch link.x

# create an application that depends on `foo`
$ cd ..
$ cargo new --bin hello && cd $_
$ cargo add foo --path ../../foo

# this won't link because the linker script is empty. that's OK; that's not the issue
$ cargo rustc -- -C link-arg=-Tlink.x
error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" (..) "-L" "/home/japaric/tmp/a/hello/target/debug/build/foo-4cfaa457c325b81c/out" (..)

# note the -L path in the linker error; the linker script *is* in that folder
# so the linker finds the linker script
$ find -name link.x
/home/japaric/tmp/a/hello/target/debug/build/foo-4cfaa457c325b81c/out/link.x
# now move the Cargo project
$ cd ../..
$ mkdir b && cd $_
$ mv ../a/hello .
$ cd hello

# re-run the `cargo rustc` command
# the linker error is different; the linker can't find `link.x` this time
$ cargo rustc -- -C link-arg=-Tlink.x
error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" (..) "-L" "/home/japaric/tmp/a/hello/target/debug/build/foo-4cfaa457c325b81c/out" (..)
  = note: /usr/bin/ld: cannot open linker script file link.x: No such file or directory

Note the linker invocation; the foo search path is the same as before. By moving the hello crate the search path generated by the foo dependency becomes invalidated.

Meta

$ cargo -V
cargo 0.20.0-nightly (cf17c9f71 2017-05-09)

Possible solution: Re-run all build scripts when the Cargo project is moved.

cc @alexcrichton @dpc
Originally reported in rust-embedded/cortex-m#31

@alexcrichton
Copy link
Member

I seem to recall we had a bug saying we shouldn't do anything when a Cargo project is moved, and now here's a bug saying we should do things when a Cargo project is moved. There's probably some way to distinguish this which we need to look into.

@carols10cents carols10cents added A-build-scripts Area: build.rs scripts A-linkage Area: linker issues, dylib, cdylib, shared libraries, so C-bug Category: bug labels Oct 3, 2017
@carols10cents
Copy link
Member

This is the related issue that says we shouldn't need to recompile when the project dir is moved: #3273

alexcrichton added a commit to alexcrichton/cargo that referenced this issue Dec 18, 2017
This commit fixes an issue in Cargo where if a project's folder was
renamed but it also contained a build script the project could break.
Cargo would continue to use the previous `rustc-link-search` arguments
to configure env vars like `LD_LIBRARY_PATH` but the literal values from
the previous compilation would be stale as the directories would no
longer be there.

To fix this when parsing the build script output we now retain a log of
the previous output directory of a build script invocation as well as
the current output, tweaking paths as appropriate if they were contained
in the output folder.

Closes rust-lang#4053
bors added a commit that referenced this issue Dec 18, 2017
Fix renaming a project using build scripts

This commit fixes an issue in Cargo where if a project's folder was
renamed but it also contained a build script the project could break.
Cargo would continue to use the previous `rustc-link-search` arguments
to configure env vars like `LD_LIBRARY_PATH` but the literal values from
the previous compilation would be stale as the directories would no
longer be there.

To fix this when parsing the build script output we now retain a log of
the previous output directory of a build script invocation as well as
the current output, tweaking paths as appropriate if they were contained
in the output folder.

Closes #4053
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-build-scripts Area: build.rs scripts A-linkage Area: linker issues, dylib, cdylib, shared libraries, so C-bug Category: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants