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

compile_rustfmt rewrite #6275

Merged
merged 42 commits into from
Oct 22, 2024
Merged

Conversation

benluiwj
Copy link
Contributor

No description provided.

check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
@ytmimi
Copy link
Contributor

ytmimi commented Aug 20, 2024

I've been thinking about what the best return type is for build_rustfmt_from_src, and this is what I've come up with. Happy to change the name, but ultimately we need to associate the LD_LIBRARY_PATH with the binary's path, and I think this would work fairly well:

pub struct RustfmtRunner {
    ld_library_path: String,
    binary_path: PathBuf,
}

impl RustfmtRunner {
    fn run(&self, args: &[&str]) -> std::io::Result<std::process::Output> {
        Command::new(&self.binary_path)
            .env("LD_LIBRARY_PATH", &self.ld_library_path)
            .args(args)
            .output()
    }
}

pub fn build_rustfmt_from_src(output_path: PathBuf) -> Result<RustfmtRunner, CheckDiffError> {
    let ld_library_path = get_ld_lib_path()?;

    Command::new("cargo")
        .args(["build", "-q", "--release", "--bin", "rustfmt"])
        .output()?;

    std::fs::copy("target/release/rustfmt", &output_path)?;

    Ok(RustfmtRunner {
        ld_library_path,
        binary_path: output_path,
    })
}

@benluiwj
Copy link
Contributor Author

@ytmimi i think the approach on build_rustfmt_from_src with RustfmtRunner is promising.

I was wondering if the error for run be an IO error or do you think its better to have a custom error for RustfmtRunner? I was thinking maybe something like GitError but for RustfmtRunner. What do you think?

Also, instead of returning Result<Output>, we return something like Result<(), RustfmtRunnerError>. How do you feel about this?

@ytmimi
Copy link
Contributor

ytmimi commented Aug 20, 2024

I was wondering if the error for run be an IO error or do you think its better to have a custom error for RustfmtRunner? I was thinking maybe something like GitError but for RustfmtRunner. What do you think?

Also, instead of returning std::io::Result<std::process::Output>, we return something like Result<(), RustfmtRunnerError>. How do you feel about this?

Personally, I don't think we need to get extremely specific with the errors. At the end of the day we're calling Command::output, and I think it's fine to just return std::io::Result<std::process::Output>.

If we ever need to convert the IO error we should be able to do that easily from the caller with the ? operator. Also, having access to the Output is going to be useful since we'll want access to stdout and stderr once we start formatting files with rustfmt.

check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
@benluiwj benluiwj force-pushed the compile-rsfmt-rewrite branch from 7e6d0d4 to 4c0d43e Compare September 13, 2024 04:05
@benluiwj benluiwj requested a review from ytmimi September 13, 2024 04:14
check_diff/src/lib.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@ytmimi ytmimi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your patience on this one. I've left several inline comments with suggestions for how we can improve this further.

check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
@benluiwj benluiwj requested a review from ytmimi September 21, 2024 04:32
Copy link
Contributor

@ytmimi ytmimi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for all your hard work on this one. I'm requesting a few changes here, and then I think we'll be good to go. One question I had is whether or not you plan on adding any unit tests? I don't think it's 100% necessary, but I wanted to ask if that was your plan.

If you make my suggested changes and add the following line to fn main() you can run the program with CHECK_DIFF_LOG=info cargo run --bin check_diff -- https://github.com/ytmimi/rustfmt.git subtree_push_automation and see the following output:

fn main() changes
fn main() {
    tracing_subscriber::fmt()
        .with_env_filter(tracing_subscriber::EnvFilter::from_env("CHECK_DIFF_LOG"))
        .init();
    // rest of the main function
}
Log Output
2024-09-26T02:00:07.108055Z  INFO check_diff: Created tmp_dir TempDir { path: "/Users/ytmimi/Documents/Projects/rust_projects/rustfmt/check_diff/.tmpGoVKYx" }
2024-09-26T02:00:08.774163Z  INFO check_diff: Successfully cloned repository https://github.com/rust-lang/rustfmt.git
2024-09-26T02:00:08.774235Z  INFO check_diff: Current directory: /Users/ytmimi/Documents/Projects/rust_projects/rustfmt/check_diff/.tmpGoVKYx
2024-09-26T02:00:08.778775Z  INFO check_diff: Successfully added remote: https://github.com/ytmimi/rustfmt.git
2024-09-26T02:00:10.284677Z  INFO check_diff: Successfully fetched subtree_push_automation.
2024-09-26T02:00:10.312499Z  INFO check_diff: Compiling with cargo 1.83.0-nightly (c1fa840a8 2024-08-29)
2024-09-26T02:00:10.345363Z  INFO check_diff: Building rustfmt from source
2024-09-26T02:00:29.191443Z  INFO check_diff: Successfully switched to subtree_push_automation
2024-09-26T02:00:29.223861Z  INFO check_diff: Building rustfmt from source
2024-09-26T02:00:37.776857Z  INFO check_diff: RUSFMT_BIN rustfmt 1.8.0-nightly (6157568e1c 2024-09-20)
2024-09-26T02:00:37.776891Z  INFO check_diff: Runtime dependencies for (src) rustfmt -- LD_LIBRARY_PATH: /Users/ytmimi/.rustup/toolchains/nightly-2024-09-10-aarch64-apple-darwin/lib
2024-09-26T02:00:38.457874Z  INFO check_diff: FEATURE_BIN rustfmt 1.7.1-nightly (87d9ff0652 2024-09-19)
2024-09-26T02:00:38.457961Z  INFO check_diff: Runtime dependencies for (feature) rustfmt -- LD_LIBRARY_PATH: /Users/ytmimi/.rustup/toolchains/nightly-2024-09-10-aarch64-apple-darwin/lib

check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
check_diff/src/lib.rs Outdated Show resolved Hide resolved
@ytmimi ytmimi force-pushed the compile-rsfmt-rewrite branch from 61aeaa1 to 4c1328c Compare October 21, 2024 23:40
@ytmimi ytmimi merged commit affb464 into rust-lang:master Oct 22, 2024
26 checks passed
@benluiwj benluiwj deleted the compile-rsfmt-rewrite branch November 9, 2024 02:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants