Fallback to message-format=json for cargo < 1.38

This commit is contained in:
Nicolas Mattia 2020-02-12 12:39:30 +01:00
parent 19bfdcf0d5
commit c435f39d6b
3 changed files with 17 additions and 4 deletions

View file

@ -46,7 +46,7 @@ it is converted to an attribute set equivalent to `{ root = theArg; }`.
| `src` | Used by `naersk` as source input to the derivation. When `root` is not set, `src` is also used to discover the `Cargo.toml` and `Cargo.lock`. |
| `root` | Used by `naersk` to read the `Cargo.toml` and `Cargo.lock` files. May be different from `src`. When `src` is not set, `root` is (indirectly) used as `src`. |
| `cargoBuild` | The command to use for the build. The argument must be a function modifying the default value. <br/> Default: `''cargo $cargo_options build $cargo_build_options >> $cargo_build_output_json''` |
| `cargoBuildOptions` | Options passed to cargo build, i.e. `cargo build <OPTS>`. These options can be accessed during the build through the environment variable `cargo_build_options`. <br/> Note: naersk relies on the `--out-dir out` option and the `--message-format=json-diagnostic-rendered-ansi` option. <br/> Note: these values are not (shell) escaped, meaning that you can use environment variables but must be careful when introducing e.g. spaces. <br/> The argument must be a function modifying the default value. <br/> Default: `[ "$cargo_release" ''-j "$NIX_BUILD_CORES"'' "--out-dir" "out" "--message-format=json-diagnostic-rendered-ansi" ]` |
| `cargoBuildOptions` | Options passed to cargo build, i.e. `cargo build <OPTS>`. These options can be accessed during the build through the environment variable `cargo_build_options`. <br/> Note: naersk relies on the `--out-dir out` option and the `--message-format` option. The `$cargo_message_format` variable is set based on the cargo version.<br/> Note: these values are not (shell) escaped, meaning that you can use environment variables but must be careful when introducing e.g. spaces. <br/> The argument must be a function modifying the default value. <br/> Default: `[ "$cargo_release" ''-j "$NIX_BUILD_CORES"'' "--out-dir" "out" "--message-format=$cargo_message_format" ]` |
| `doCheck` | When true, `checkPhase` is run. Default: `true` |
| `cargoTestCommands` | The commands to run in the `checkPhase`. The argument must be a function modifying the default value. <br/> Default: `[ ''cargo $cargo_options test $cargo_test_options'' ]` |
| `cargoTestOptions` | Options passed to cargo test, i.e. `cargo test <OPTS>`. These options can be accessed during the build through the environment variable `cargo_test_options`. <br/> Note: these values are not (shell) escaped, meaning that you can use environment variables but must be careful when introducing e.g. spaces. <br/> The argument must be a function modifying the default value. <br/> Default: `[ "$cargo_release" ''-j "$NIX_BUILD_CORES"'' ]` |

View file

@ -202,7 +202,19 @@ let
}
cargo_build_output_json=$(mktemp)
cargo_version=$(cargo --version | grep -oP 'cargo \K.*')
# ANSI rendered diagnostics were introduced in 1.38:
# https://github.com/rust-lang/cargo/blob/master/CHANGELOG.md#cargo-138-2019-09-26
if ! [[ "$cargo_version" < "1.38" ]]
then
cargo_message_format="json-diagnostic-rendered-ansi"
else
cargo_message_format="json"
fi
log "cargo_version (read): $cargo_version"
log "cargo_message_format (set): $cargo_message_format"
log "cargo_release: $cargo_release"
log "cargo_options: $cargo_options"
log "cargo_build_options: $cargo_build_options"
@ -253,7 +265,7 @@ let
if [ "$cargo_ec" -ne "0" ]
then
cat $cargo_build_output_json | jq -cMr 'select(.message.rendered != null) | .message.rendered'
cat "$cargo_build_output_json" | jq -cMr 'select(.message.rendered != null) | .message.rendered'
log "cargo returned with exit code $cargo_ec, exiting"
exit "$cargo_ec"
fi

View file

@ -34,11 +34,12 @@ let
# can be accessed during the build through the environment variable
# `cargo_build_options`. <br/>
# Note: naersk relies on the `--out-dir out` option and the
# `--message-format=json-diagnostic-rendered-ansi` option. <br/>
# `--message-format` option. The `$cargo_message_format` variable is set
# based on the cargo version.<br/>
# Note: these values are not (shell) escaped, meaning that you can use
# environment variables but must be careful when introducing e.g. spaces. <br/>
cargoBuildOptions =
allowFun attrs0 "cargoBuildOptions" [ "$cargo_release" ''-j "$NIX_BUILD_CORES"'' "--out-dir" "out" "--message-format=json-diagnostic-rendered-ansi" ];
allowFun attrs0 "cargoBuildOptions" [ "$cargo_release" ''-j "$NIX_BUILD_CORES"'' "--out-dir" "out" "--message-format=$cargo_message_format" ];
# When true, `checkPhase` is run.
doCheck = attrs0.doCheck or true;