Repository: nushell/nushell
File: crates/nu-cli/src/eval_file.rs (Rust)

Code before:
                let cmdline = format!("nu {path} {}", args.join(" "));
                let mut working_set = StateWorkingSet::new(engine_state);
                let file_id = working_set.add_file("<commandline>".into(), cmdline.as_bytes());
                let span = working_set
                    .get_span_for_file(file_id)
                    .subspan(3, path.len() + 3)
                    .expect("<commandline> to contain script path");
                engine_state.merge_delta(working_set.render())?;
                let e = IoError::new(err.not_found_as(NotFound::File), span, PathBuf::from(&path));
                Err(e)
            }
        }
    }?;

    let file_path_str = file_path
        .to_str()
        .ok_or_else(|| ShellError::NonUtf8Custom {
            msg: format!(
                "Input file name '{}' is not valid UTF8",
                file_path.to_string_lossy()
            ),
            span: Span::unknown(),
        })?;

    let file = std::fs::read(&file_path).map_err(|err| {
        IoError::new_internal_with_path(
            err.not_found_as(NotFound::File),
            "Could not read file",
            nu_protocol::location!(),
            file_path.clone(),
        )
    })?;
    engine_state.file = Some(file_path.clone());

    let parent = file_path.parent().ok_or_else(|| {
        IoError::new_internal_with_path(
            ErrorKind::DirectoryNotFound,
            "The file path does not have a parent",
            nu_protocol::location!(),
            file_path.clone(),
        )
    })?;

    stack.add_env_var(
        "FILE_PWD".to_string(),
        Value::string(parent.to_string_lossy(), Span::unknown()),
    );
    stack.add_env_var(
        "CURRENT_FILE".to_string(),
        Value::string(file_path.to_string_lossy(), Span::unknown()),
    );

Review comment (bug):
I realize this is just moved code, but I wonder if eliminating expect here and having a nushell error would be more helpful. otoh, this may just never fail?

Code after:
                err,
                "Invalid path",
                nu_protocol::location!(),
                PathBuf::from(&path),
            )),
        }
    }?;

    let file_path_str = file_path
        .to_str()
        .ok_or_else(|| ShellError::NonUtf8Custom {
            msg: format!(
                "Input file name '{}' is not valid UTF8",
                file_path.to_string_lossy()
            ),
            span: Span::unknown(),
        })?;

    let file = std::fs::read(&file_path).map_err(|err| {
        let cmdline = format!("nu {path} {}", args.join(" "));
        let mut working_set = StateWorkingSet::new(engine_state);
        let file_id = working_set.add_file("<commandline>".into(), cmdline.as_bytes());
        let span = working_set
            .get_span_for_file(file_id)
            .subspan(3, path.len() + 3)
            .expect("<commandline> to contain script path");
        if let Err(err) = engine_state.merge_delta(working_set.render()) {
            err
        } else {
            IoError::new(err.not_found_as(NotFound::File), span, PathBuf::from(&path)).into()
        }
    })?;
    engine_state.file = Some(file_path.clone());

    let parent = file_path.parent().ok_or_else(|| {
        IoError::new_internal_with_path(
            ErrorKind::DirectoryNotFound,
            "The file path does not have a parent",
            nu_protocol::location!(),
            file_path.clone(),
        )
    })?;

    stack.add_env_var(
        "FILE_PWD".to_string(),
        Value::string(parent.to_string_lossy(), Span::unknown()),
    );
    stack.add_env_var(
        "CURRENT_FILE".to_string(),
        Value::string(file_path.to_string_lossy(), Span::unknown()),
    );