Skip to content

Commit

Permalink
Ensure currently_parsed_cwd is set for config files (nushell#12338)
Browse files Browse the repository at this point in the history
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx

you can also mention related issues, PRs or discussions!
-->

# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.

Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->

Fixes nushell#7849, nushell#11465 based on @kubouch's suggestion in
nushell#11465 (comment).

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

Can source files relative to `env.nu` or `config.nu` like in nushell#6150.

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

Adds test that previously failed.

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
  • Loading branch information
texastoland authored and NotTheDr01ds committed Apr 12, 2024
1 parent e945894 commit 7ec4f55
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
7 changes: 7 additions & 0 deletions crates/nu-cli/src/config_files.rs
Expand Up @@ -91,6 +91,10 @@ pub fn eval_config_contents(
let config_filename = config_path.to_string_lossy();

if let Ok(contents) = std::fs::read(&config_path) {
// Change currently parsed directory
let prev_currently_parsed_cwd = engine_state.currently_parsed_cwd.clone();
engine_state.start_in_file(Some(&config_filename));

eval_source(
engine_state,
stack,
Expand All @@ -100,6 +104,9 @@ pub fn eval_config_contents(
false,
);

// Restore the currently parsed directory back
engine_state.currently_parsed_cwd = prev_currently_parsed_cwd;

// Merge the environment in case env vars changed in the config
match nu_engine::env::current_dir(engine_state, stack) {
Ok(cwd) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-protocol/src/engine/engine_state.rs
Expand Up @@ -99,7 +99,7 @@ pub struct EngineState {
pub history_enabled: bool,
pub history_session_id: i64,
// If Nushell was started, e.g., with `nu spam.nu`, the file's parent is stored here
pub(super) currently_parsed_cwd: Option<PathBuf>,
pub currently_parsed_cwd: Option<PathBuf>,
pub regex_cache: Arc<Mutex<LruCache<String, Regex>>>,
pub is_interactive: bool,
pub is_login: bool,
Expand Down
9 changes: 9 additions & 0 deletions tests/parsing/mod.rs
Expand Up @@ -13,6 +13,15 @@ fn source_file_relative_to_file() {
assert_eq!(actual.out, "5");
}

#[test]
fn source_file_relative_to_config() {
let actual = nu!("
nu --config tests/parsing/samples/source_file_relative.nu --commands ''
");

assert_eq!(actual.out, "5");
}

#[test]
fn source_const_file() {
let actual = nu!(cwd: "tests/parsing/samples",
Expand Down

0 comments on commit 7ec4f55

Please sign in to comment.