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

Strange behaviour of source command in config.nu #4773

Closed
ajeetdsouza opened this issue Mar 7, 2022 · 11 comments
Closed

Strange behaviour of source command in config.nu #4773

ajeetdsouza opened this issue Mar 7, 2022 · 11 comments

Comments

@ajeetdsouza
Copy link

Describe the bug

There are a couple of problems with the source command when I use it in my config.nu.


Suppose the file ~/foo does not exist. Then the following config.nu file fails, saying that the file does not exist:

echo "some code" | save ~/foo
source ~/foo

However, the file should exist, because I just created it. However, when I go to check, I see that the file has not been created either.


Suppose the file ~/foo had the contents "some code". Now, consider the following config.nu file:

echo "other code" | save ~/foo
source ~/foo

This should have sourced other code, because that's what I just wrote to the file. However, Nushell (incorrectly) ends up loading some code. When I check, I see that ~/foo (correctly) contains other code.


Finally, source does not work with variables. This is strange behaviour, considering that everything else in Nushell does.

How to reproduce

N/A

Expected behavior

N/A

Screenshots

No response

Configuration

key value
version 0.59.0
branch main
short_commit 42f1874
commit_hash 42f1874
commit_date 2022-03-01 13:05:29 +00:00
build_os macos-x86_64
rust_version rustc 1.59.0 (9d1b2106e 2022-02-23)
rust_channel stable-x86_64-apple-darwin
cargo_version cargo 1.59.0 (49d8809dc 2022-02-10)
pkg_version 0.59.0
build_time 2022-03-01 13:39:18 +00:00
build_rust_channel release
features dataframe, default, trash, which, zip
installed_plugins

Additional context

These issues are blockers for the implementation of the zoxide plugin on Nushell.

CC @fdncred

@fdncred
Copy link
Collaborator

fdncred commented Mar 7, 2022

What you're trying to do may not be do-able in nushell. It's my understanding that writing a file happens at 'eval time' and sourcing a file happens at 'parse time' and I don't believe those two things ever mix. I'm hoping we can come up with some other solution that fits both nushell and zoxide. Nushell's architecture is just different and we don't try to be POSIX-like so source in nushell doesn't really work like source in bash.

@ajeetdsouza
Copy link
Author

The basic idea is that zoxide init nushell generates a script which I want Nushell to source, I'd love to hear if there are alternate ways of doing so.

@fdncred
Copy link
Collaborator

fdncred commented Mar 7, 2022

yup, understood. I still have this in my nushell v0.44 config.

startup = [
    "zoxide init nushell --hook prompt | save ~/.zoxide.nu",
    "source ~/.zoxide.nu",

@kubouch
Copy link
Contributor

kubouch commented Mar 8, 2022

@ajeetdsouza Would it be possible to add zoxide init nushell --hook prompt | save ~/.zoxide.nu into installation instructions and have only source ~/.zoxide.nu in the config? Does the zoxide.nu need to be rewritten on every Nushell startup?

Unfortunately, as @fdncred said, it's a language design issue that's not easy to fix -- allowing it would have deep consequences and disallow many assumptions we currently rely on.

@ajeetdsouza
Copy link
Author

@kubouch while that would work, it would break every time zoxide was upgraded. I don't want to rely on package managers to rewrite the script each time, moreso for a fast-changing shell like Nushell.

@kubouch
Copy link
Contributor

kubouch commented Mar 8, 2022

@ajeetdsouza I see. The way you described (save and source file within the same script) is currently impossible. However, there might be a workaround: Within zoxide.nu you would define environment variable, e.g., let-env ZOXIDE_VERSION = '0.7.0' and a custom command along these lines:

def zoxide-check-updates [] {
    let actual_version = (zoxide --version | str trim)
    if $actual_version != $env.ZOXIDE_VERSION {
        zoxide init nushell --hook prompt | save ~/.zoxide.nu
        print $'Zoxide was upgraded from ($env.ZOXIDE_VERSION) to ($actual_version), please reload Nushell'
    }
}

Then, you could instruct users to put this to the config.nu:

source ~/.zoxide.nu
zoxide-check-updates

You would need to run zoxide init nushell --hook prompt | save ~/.zoxide.nu when you install zoxide for the first time. But after that, it should keep the script up to date and notify users when it changed. The advantage is that you don't write the file on every startup.

@ajeetdsouza
Copy link
Author

@kubouch that's still a bit of a problem:

  • Nushell updates its syntax quite often. If a breaking change occurs, the source command breaks and then zoxide-check-updates doesn't run.
  • zoxide's generated script also changes based on user settings. Regenerating the script based on version number would not work here.

I suppose I could ask users to rerun this line every time they change settings or update Nushell, but I was trying for a method with less friction.

Thanks for your help! It's unfortunate that the source command needs to statically load completions -- even outside of zoxide, an increasing number of programs are using the foo completions | source pattern rather than bundling completions as a separate script. I imagine the reason source works the way it does is that type-checking occurs in the parser. I wonder if moving it to the interpreter would be possible / a good idea.

If this is impossible, let me know, so I can mark this issue as resolved.

@kubouch
Copy link
Contributor

kubouch commented Mar 8, 2022

Ah, I see. I'm afraid regenerating the file after each change might be the best solution now. Currently, we don't have plans to change how source works since that would change everything.

davidkna added a commit to davidkna/starship that referenced this issue Mar 23, 2022
davidkna added a commit to davidkna/starship that referenced this issue Mar 23, 2022
@ajeetdsouza
Copy link
Author

I managed to fix this by adding echo "some code" | save ~/foo to env.nu and source ~/foo to config.nu: ajeetdsouza/zoxide@e06c830

CC @davidkna perhaps Starship can do something similar?

@hustcer
Copy link
Contributor

hustcer commented May 30, 2022

@ajeetdsouza Since you have already found a workaround, and https://www.nushell.sh/book/thinking_in_nushell.html#think-of-nushell-as-a-compiled-language, can we close this issue?

@ajeetdsouza
Copy link
Author

Absolutely, I think we can close this.

@hustcer hustcer closed this as completed May 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants