-
Notifications
You must be signed in to change notification settings - Fork 13.4k
run-make: arm command wrappers with drop bombs #125752
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7255c28
run-make-support: remove `env_var`
jieyouxu a3feeb3
run-make-support: add drop bomb module
jieyouxu 54e7044
run-make-support: arm command with drop bombs
jieyouxu ca95f78
tests/run-make: update tests to use new API
jieyouxu d308a70
run-make-support: bump version
jieyouxu 5ec3eef
run-make-support: add changelog
jieyouxu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Changelog | ||
|
||
All notable changes to the `run_make_support` library should be documented in this file. | ||
|
||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the support | ||
library should adhere to [Semantic Versioning](https://semver.org/spec/v2.0.0.html) even if it's | ||
not intended for public consumption (it's moreso to help internally, to help test writers track | ||
changes to the support library). | ||
|
||
This support library will probably never reach 1.0. Please bump the minor version in `Cargo.toml` if | ||
you make any breaking changes or other significant changes, or bump the patch version for bug fixes. | ||
|
||
## [0.1.0] - 2024-06-09 | ||
|
||
### Changed | ||
|
||
- Use *drop bombs* to enforce that commands are executed; a command invocation will panic if it is | ||
constructed but never executed. Execution methods `Command::{run, run_fail}` will defuse the drop | ||
bomb. | ||
- Added `Command` helpers that forward to `std::process::Command` counterparts. | ||
|
||
### Removed | ||
|
||
- The `env_var` method which was incorrectly named and is `env_clear` underneath and is a footgun | ||
from `impl_common_helpers`. For example, removing `TMPDIR` on Unix and `TMP`/`TEMP` breaks | ||
`std::env::temp_dir` and wrecks anything using that, such as rustc's codgen. | ||
- Removed `Deref`/`DerefMut` for `run_make_support::Command` -> `std::process::Command` because it | ||
causes a method chain like `htmldocck().arg().run()` to fail, because `arg()` resolves to | ||
`std::process::Command` which also returns a `&mut std::process::Command`, causing the `run()` to | ||
be not found. | ||
|
||
## [0.0.0] - 2024-06-09 | ||
|
||
Consider this version to contain all changes made to the support library before we started to track | ||
changes in this changelog. | ||
|
||
### Added | ||
|
||
- Custom command wrappers around `std::process::Command` (`run_make_support::Command`) and custom | ||
wrapper around `std::process::Output` (`CompletedProcess`) to make it more convenient to work with | ||
commands and their output, and help avoid forgetting to check for exit status. | ||
- `Command`: `set_stdin`, `run`, `run_fail`. | ||
- `CompletedProcess`: `std{err,out}_utf8`, `status`, `assert_std{err,out}_{equals, contains, | ||
not_contains}`, `assert_exit_code`. | ||
- `impl_common_helpers` macro to avoid repeating adding common convenience methods, including: | ||
- Environment manipulation methods: `env`, `env_remove` | ||
- Command argument providers: `arg`, `args` | ||
- Common invocation inspection (of the command invocation up until `inspect` is called): | ||
`inspect` | ||
- Execution methods: `run` (for commands expected to succeed execution, exit status `0`) and | ||
`run_fail` (for commands expected to fail execution, exit status non-zero). | ||
- Command wrappers around: `rustc`, `clang`, `cc`, `rustc`, `rustdoc`, `llvm-readobj`. | ||
- Thin helpers to construct `python` and `htmldocck` commands. | ||
- `run` and `run_fail` (like `Command::{run, run_fail}`) for running binaries, which sets suitable | ||
env vars (like `LD_LIB_PATH` or equivalent, `TARGET_RPATH_ENV`, `PATH` on Windows). | ||
- Pseudo command `diff` which has similar functionality as the cli util but not the same API. | ||
- Convenience panic-on-fail helpers `env_var`, `env_var_os`, `cwd` for their `std::env` conterparts. | ||
- Convenience panic-on-fail helpers for reading respective env vars: `target`, `source_root`. | ||
- Platform check helpers: `is_windows`, `is_msvc`, `cygpath_windows`, `uname`. | ||
- fs helpers: `copy_dir_all`. | ||
- `recursive_diff` helper. | ||
- Generic `assert_not_contains` helper. | ||
- Scoped run-with-teardown helper `run_in_tmpdir` which is designed to run commands in a temporary | ||
directory that is cleared when closure returns. | ||
- Helpers for constructing the name of binaries and libraries: `rust_lib_name`, `static_lib_name`, | ||
`bin_name`, `dynamic_lib_name`. | ||
- Re-export libraries: `gimli`, `object`, `regex`, `wasmparsmer`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "run_make_support" | ||
version = "0.0.0" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
//! This module implements "drop bombs" intended for use by command wrappers to ensure that the | ||
//! constructed commands are *eventually* executed. This is exactly like `rustc_errors::Diag` where | ||
//! we force every `Diag` to be consumed or we emit a bug, but we panic instead. | ||
//! | ||
//! This is adapted from <https://docs.rs/drop_bomb/latest/drop_bomb/> and simplified for our | ||
//! purposes. | ||
|
||
use std::ffi::{OsStr, OsString}; | ||
use std::panic; | ||
|
||
#[cfg(test)] | ||
mod tests; | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct DropBomb { | ||
command: OsString, | ||
defused: bool, | ||
armed_line: u32, | ||
} | ||
|
||
impl DropBomb { | ||
/// Arm a [`DropBomb`]. If the value is dropped without being [`defused`][Self::defused], then | ||
/// it will panic. It is expected that the command wrapper uses `#[track_caller]` to help | ||
/// propagate the caller info from rmake.rs. | ||
#[track_caller] | ||
pub(crate) fn arm<S: AsRef<OsStr>>(command: S) -> DropBomb { | ||
DropBomb { | ||
command: command.as_ref().into(), | ||
defused: false, | ||
armed_line: panic::Location::caller().line(), | ||
} | ||
} | ||
|
||
/// Defuse the [`DropBomb`]. This will prevent the drop bomb from panicking when dropped. | ||
pub(crate) fn defuse(&mut self) { | ||
self.defused = true; | ||
} | ||
} | ||
|
||
impl Drop for DropBomb { | ||
fn drop(&mut self) { | ||
if !self.defused && !std::thread::panicking() { | ||
panic!( | ||
"command constructed but not executed at line {}: `{}`", | ||
self.armed_line, | ||
self.command.to_string_lossy() | ||
) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.