Skip to content

Using rustc as a library hits "can't find crate for std" error #11792

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

Closed
huonw opened this issue Jan 25, 2014 · 6 comments
Closed

Using rustc as a library hits "can't find crate for std" error #11792

huonw opened this issue Jan 25, 2014 · 6 comments

Comments

@huonw
Copy link
Member

huonw commented Jan 25, 2014

The following is the parse, expand & analyse steps of the compiler (cribbed from rustdoc). Running on any non-#[no_std] file causes the following error:

rustc_lib_example.rs:1:1: 1:1 error: can't find crate for `std`
rustc_lib_example.rs:1 #[feature(managed_boxes)];
                       ^
#[feature(managed_boxes)];
extern mod rustc;
extern mod syntax;

use std::os;
use rustc::driver::{driver, session};
use rustc::metadata::creader::Loader;
use syntax::diagnostic;

/// Extract the expanded ast of a crate, along with the codemap which
/// connects source code locations to the actual code.
fn main() {
    let path = Path::new(os::args()[1]);

    let parsesess = syntax::parse::new_parse_sess(None);
    let input = driver::FileInput(path);

    let sessopts = @session::Options {
        binary: ~"example",
        .. (*session::basic_options()).clone()
    };


    let diagnostic_handler = diagnostic::mk_handler(None);
    let span_diagnostic_handler =
        diagnostic::mk_span_handler(diagnostic_handler, parsesess.cm);

    let sess = driver::build_session_(sessopts, parsesess.cm,
                                      @diagnostic::DefaultEmitter as @diagnostic::Emitter,
                                      span_diagnostic_handler);

    let cfg = driver::build_configuration(sess);

    let crate = driver::phase_1_parse_input(sess, cfg.clone(), &input);

    let loader = &mut Loader::new(sess);
    let (crate, ast_map) = driver::phase_2_configure_and_expand(sess, cfg, loader, crate);
    driver::phase_3_run_analysis_passes(sess, &crate, ast_map);
}

This makes rustc completely unusable as a library for out-of-source-tree tools.

@alexcrichton
Copy link
Member

One possible solution to this is #11068. The problem is that librustc will attempt to guess the current "sysroot" based on the path to the executable.

I wouldn't be so harsh as to say that rustc is completely unusable, you'll just need to explicitly specify a path or two for now, more a minor annoyance than a fundamental blocker.

@huonw
Copy link
Member Author

huonw commented Jan 25, 2014

I think it's more than minor: I don't know of a feasible way of getting the correct path cross-platform, and there's not even a sensible way of doing it on a single platform... some people install to /usr/local/..., others install to $HOME/..., sometimes just directly compile with $BUILD_DIR/x86-64_.../stage2/bin/rustc directly, without putting it in the PATH.

@jaredly
Copy link

jaredly commented Nov 18, 2014

@alexcrichton where would I specify that path? I'm running into this problem now (code here https://gist.github.com/jaredly/089b60e0649c4bda3cd9)

@alexcrichton
Copy link
Member

I believe it's the maybe_sysroot option of Options

@steveklabnik
Copy link
Member

Triage: this code has changed significantly, but I'm not sure if anything else has changed in the meantime to fix this.

@aturon
Copy link
Member

aturon commented Apr 15, 2017

Closing due to inactivity. Feel free to comment/reopen if this problem is actively causing you pain.

@aturon aturon closed this as completed Apr 15, 2017
flip1995 pushed a commit to flip1995/rust that referenced this issue Nov 16, 2023
[`map_identity`]: respect match ergonomics

Fixes rust-lang#11764

Note: the original tests before this were slightly wrong themselves already and had to be changed. They were calling `map` on an iterator of `&(i32, i32)`s, so this PR would stop linting there, but they were meant to test something else unrelated to binding modes, so I just changed them to remove the references so that it iterates over owned values and they all bind by value. This way they continue to test what they checked for before: the identity function for tuple patterns.

changelog: [`map_identity`]: respect match ergonomics
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants