Skip to content

nu script doesn't have access to config functions #5544

Closed

Description

@danjenson

Describe the bug

I have the following script

#!/usr/bin/env nu
gdrive sync . class/simulation/lectures/

Where gdrive sync is defined in my config.nu, but if I run this as a linux script, it doesn't find the command.

In bash, you can do export -f <func> -- is there anything similar for nushell?

How to reproduce

Create a UDF in config.nu.
Try to use it in a script.

Expected behavior

I would be able to use UDFs the same as built-ins.

Screenshots

No response

Configuration

key value
version 0.62.0
branch
tag
build_os linux-x86_64
rust_version rustc 1.61.0-nightly (ab0c2e18d 2022-03-27)
rust_channel nightly-x86_64-unknown-linux-gnu
cargo_version cargo 1.61.0-nightly (109bfbd 2022-03-17)
pkg_version 0.62.0
build_time 2022-05-03 18:02:54 -07:00
build_rust_channel release
features database, dataframe, default, trash, which, zip
installed_plugins

Additional context

No response

Activity

  1. fdncred commented on May 14, 2022

    @fdncred
    Contributor

    I'm wondering if nushell is loading the config.nu and env.nu when running a nu script like this.

  2. kubouch commented on May 15, 2022

    @kubouch
    Contributor

    Yeah, when you run a script like nu script.nu, it won't read the config.

  3. WindSoilder commented on May 20, 2022

    @WindSoilder
    Contributor

    Is it ok to load config whatever using nu like nu script.nu and nu -c ls?

  4. fdncred commented on May 20, 2022

    @fdncred
    Contributor

    Technically, there is a config loaded because I used it with the --table-mode functionality. You can see it in crates/nu-cli/src/commands.rs in the evaluate_commands() function. It's just that this all happens in a separate part of code where the "real" config doesn't load.

    I'm not sure we want to load the config or not.

  5. WindSoilder commented on May 20, 2022

    @WindSoilder
    Contributor

    Hmm... It seems that only you provide --table-mode, the config will be loaded.

    For the issue case, I think it's going to evaluate_file, which is not loading config file too.

    I'm not sure we want to load the config or not.

    I'm not sure either, If we always load the config, I don't know if there is a potential case we need to handle.

  6. danjenson commented on May 21, 2022

    @danjenson
    Author

    Why not make it an opt-in when running scripts or provide a -l,--library command that loads functions from a given file?

  7. kubouch commented on May 21, 2022

    @kubouch
    Contributor

    One argument against loading config by default when running scripts is portability. If you rely on implicit loading of your local config and send the script to your friend's machine, the friend won't be able to run it.

    We already have --config and --env-config flags to nu so maybe we could change it that when you specify them, it would load the config but otherwise not?

  8. DejayRezme commented on May 21, 2022

    @DejayRezme

    powershell loads the config / profile.ps1 when executing a command.
    What does bash do?

    I suspect if a script depends on specific modules or functions to run then that will be tricky either way, and not loading the config doesn't really help.

  9. danjenson commented on May 21, 2022

    @danjenson
    Author

    I like that idea @kubouch , and it seems natural -- I tried specifying those next to see if they worked

  10. fdncred commented on May 21, 2022

    @fdncred
    Contributor

    ya, I think --config and --env-config is a good compromise too. Who wants to PR that? LOL

  11. WindSoilder commented on May 22, 2022

    @WindSoilder
    Contributor

    Hmm... Is that means the following?

    1. if --config is specified without value, load default config
    2. if --config is specified with value, like --config a.nu, load a.nu as config file, but don't load default config.
    3. if --config is not provided, don't load config?
  12. kubouch commented on May 22, 2022

    @kubouch
    Contributor

    I think the way signature parsing works in Nushell is that either you have a flag --config or a --config file.nu with a required value. So for a start we could just go for always requiring a value after --config.

  13. hustcer commented on May 23, 2022

    @hustcer
    Contributor

    Thanks @WindSoilder , nu --config my/config.nu -c 'custom-command' works on the latest main, I have just tested it on my mac. Feel free to reopen if it doesn't work for you

  14. added this to the v0.63.0 milestone on May 23, 2022
  15. modified the milestones: v0.63.0, v0.66.0 on Jul 10, 2022
  16. kubouch commented on Jul 14, 2022

    @kubouch
    Contributor

    So the solution here is to pass a --config flag manually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions