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

Don't use process::exit in bootstrap tests #98830

Closed
jyn514 opened this issue Jul 3, 2022 · 7 comments · Fixed by #98994
Closed

Don't use process::exit in bootstrap tests #98830

jyn514 opened this issue Jul 3, 2022 · 7 comments · Fixed by #98994
Assignees
Labels
A-contributor-roadblock Area: Makes things more difficult for new contributors to rust itself E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-help-wanted Call for participation: Help is requested to fix this issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap)

Comments

@jyn514
Copy link
Member

jyn514 commented Jul 3, 2022

Bootstrap has a lot of places where it calls process::exit. This makes it hard to figure out why tests fail. We should use unique exit codes so it's easier to tell where something wrong (and maybe consider using resume_unwind instead? not sure).

$ rg exit'\(' src/bootstrap/*.rs
src/bootstrap/util.rs
338:        std::process::exit(1);
377:        std::process::exit(1);
467:    std::process::exit(1);

src/bootstrap/toolstate.rs
96:    std::process::exit(3);
111:            std::process::exit(1);
182:            std::process::exit(1);
228:            std::process::exit(1);

src/bootstrap/tool.rs
207:                exit(1);

src/bootstrap/setup.rs
97:        std::process::exit(1);
290:            std::process::exit(1);

src/bootstrap/sanity.rs
107:            std::process::exit(1);

src/bootstrap/test.rs
676:            std::process::exit(1);
1024:                std::process::exit(1);
1254:            std::process::exit(1);

src/bootstrap/lib.rs
719:            process::exit(1);
1617:                std::process::exit(1);

src/bootstrap/format.rs
35:            std::process::exit(1);
117:        std::process::exit(1);

src/bootstrap/flags.rs
257:                process::exit(exit_code);
343:            process::exit(exit_code);
375:            process::exit(1);
596:                        std::process::exit(1);
610:                process::exit(1);
800:            process::exit(1);

src/bootstrap/compile.rs
1380:        exit(1);

src/bootstrap/config.rs
820:                    process::exit(2);
1490:        exit(1);

src/bootstrap/builder.rs
350:            std::process::exit(1);
1010:            std::process::exit(1);
1439:                    std::process::exit(1);
@jyn514 jyn514 added T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) A-contributor-roadblock Area: Makes things more difficult for new contributors to rust itself labels Jul 3, 2022
@Mark-Simulacrum
Copy link
Member

Mark-Simulacrum commented Jul 3, 2022

I don't think unique exit codes are a good idea -- too much hassle; we should probably replace the exit's with a call to something like crate::exit which can panic! in cfg(test) and resume_unwind in regular code; the goal of the exits is really just to more cleanly exit than the big panic message you get otherwise.

@jyn514 jyn514 changed the title Use unique exit codes in bootstrap Don't use process::exit in bootstrap tests Jul 3, 2022
@jyn514 jyn514 added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. E-help-wanted Call for participation: Help is requested to fix this issue. labels Jul 4, 2022
@kons-9
Copy link
Contributor

kons-9 commented Jul 5, 2022

@rustbot claim

@kons-9
Copy link
Contributor

kons-9 commented Jul 6, 2022

Thank you for mentering!
From the above exchange, I thought I could write code like this.

// exit.rs
fn detail_exit(message:String){
    if (test) {
        panic!(message)
    } else (not test) {
        resume_unwind(Box::new(message))
    }
}

Is this understanding correct or incorrect?
(I've never used resume_unwind that way, so I'm not sure. but it can stop running without panic message like std::process::exit and if needed, we can get imformation about error message.)

@jyn514
Copy link
Member Author

jyn514 commented Jul 6, 2022

@kons-9 exit is fine to keep using when we're not running tests. Otherwise that looks broadly correct. The magic built-in to replace (test) with is cfg!(test): https://doc.rust-lang.org/stable/std/macro.cfg.html

I would put this in lib.rs or utils.rs, no need to add a new file just for one function.

@jyn514
Copy link
Member Author

jyn514 commented Jul 6, 2022

Ah, I missed that Mark suggested resume_unwind earlier - ok, that's fine to use, it will make sure tempfiles are deleted and that sort of thing.

@kons-9
Copy link
Contributor

kons-9 commented Jul 6, 2022

Thank you for your reply!

no need to add a new file just for one function.

Copy that!

I am going to use #[cfg(test)] in that code, but would cfg!(test) be better? (you might have suggested this macro because of my code. )

@jyn514
Copy link
Member Author

jyn514 commented Jul 6, 2022

@kons-9 I think cfg!(test) is better so you don't have to run x test src/bootstrap before you get type errors.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jul 7, 2022
replace process exit with more detailed exit in src/bootstrap/*.rs

Fixes [rust-lang#98830](rust-lang#98830)

I implemeted "detail_exit.rs" in lib.rs, and replace all of std::process::exit.
So, error code should panic in test code.
```
// lib.rs
pub fn detail_exit(code: i32) -> ! {
    // Successful exit
    if code == 0 {
        std::process::exit(0);
    }
    if cfg!(test) {
        panic!("status code: {}", code);
    } else {
        std::panic::resume_unwind(Box::new(code));
    }
}
```

<details>
<summary>% rg "exit\(" src/bootstrap/*.rs</summary>

```
builder.rs
351:            crate::detail_exit(1);
1000:            crate::detail_exit(1);
1429:                    crate::detail_exit(1);

compile.rs
1331:        crate::detail_exit(1);

config.rs
818:                    crate::detail_exit(2);
1488:        crate::detail_exit(1);

flags.rs
263:                crate::detail_exit(exit_code);
349:            crate::detail_exit(exit_code);
381:            crate::detail_exit(1);
602:                        crate::detail_exit(1);
616:                crate::detail_exit(1);
807:            crate::detail_exit(1);

format.rs
35:            crate::detail_exit(1);
117:        crate::detail_exit(1);

lib.rs
714:            detail_exit(1);
1620:                detail_exit(1);
1651:pub fn detail_exit(code: i32) -> ! {
1654:        std::process::exit(0);

sanity.rs
107:            crate::detail_exit(1);

setup.rs
97:        crate::detail_exit(1);
290:            crate::detail_exit(1);

test.rs
676:            crate::detail_exit(1);
1024:                crate::detail_exit(1);
1254:            crate::detail_exit(1);

tool.rs
207:                crate::detail_exit(1);

toolstate.rs
96:    crate::detail_exit(3);
111:            crate::detail_exit(1);
182:            crate::detail_exit(1);
228:            crate::detail_exit(1);

util.rs
339:        crate::detail_exit(1);
378:        crate::detail_exit(1);
468:    crate::detail_exit(1);
```
</details>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-contributor-roadblock Area: Makes things more difficult for new contributors to rust itself E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-help-wanted Call for participation: Help is requested to fix this issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants