Skip to content

Commit

Permalink
Fix nix shebang interaction with #8131 overhaul completions
Browse files Browse the repository at this point in the history
  • Loading branch information
roberth authored and tomberek committed Nov 7, 2023
1 parent e91fd83 commit ffd414e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 29 deletions.
4 changes: 3 additions & 1 deletion src/libcmd/installables.cc
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,9 @@ void completeFlakeRefWithFragment(
prefixRoot = ".";
}
auto flakeRefS = std::string(prefix.substr(0, hash));
auto flakeRef = parseFlakeRef(expandTilde(flakeRefS), absPath(getCommandBaseDir()));

// TODO: ideally this would use the command base directory instead of assuming ".".
auto flakeRef = parseFlakeRef(expandTilde(flakeRefS), absPath("."));

auto evalCache = openEvalCache(*evalState,
std::make_shared<flake::LockedFlake>(lockFlake(*evalState, flakeRef, lockFlags)));
Expand Down
19 changes: 8 additions & 11 deletions src/libutil/args.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,6 @@ std::optional<std::string> RootArgs::needsCompletion(std::string_view s)
return {};
}

void RootArgs::parseCmdline(const Strings & _cmdline)
{
// Default via 5.1.2.2.1 in C standard
Args::parseCmdline(_cmdline, false);
}

/**
* Basically this is `typedef std::optional<Parser> Parser(std::string_view s, Strings & r);`
*
Expand Down Expand Up @@ -227,7 +221,7 @@ static Strings parseShebangContent(std::string_view s) {
return result;
}

void Args::parseCmdline(const Strings & _cmdline, bool allowShebang)
void RootArgs::parseCmdline(const Strings & _cmdline, bool allowShebang)
{
Strings pendingArgs;
bool dashDash = false;
Expand Down Expand Up @@ -339,10 +333,13 @@ void Args::parseCmdline(const Strings & _cmdline, bool allowShebang)

Path Args::getCommandBaseDir() const
{
if (parent)
return parent->getCommandBaseDir();
else
return commandBaseDir;
assert(parent);
return parent->getCommandBaseDir();
}

Path RootArgs::getCommandBaseDir() const
{
return commandBaseDir;
}

bool Args::processFlag(Strings::iterator & pos, Strings::iterator end)
Expand Down
17 changes: 1 addition & 16 deletions src/libutil/args.hh
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,9 @@ class AddCompletions;

class Args
{
/**
* @brief The command's "working directory", but only set when top level.
*
* Use getCommandBaseDir() to get the directory regardless of whether this
* is a top-level command or subcommand.
*
* @see getCommandBaseDir()
*/
Path commandBaseDir = ".";

public:

/**
* Parse the command line with argv0, throwing a UsageError if something
goes wrong.
*/
void parseCmdline(const Strings & _cmdline, bool allowShebang);

/**
* Return a short one-line description of the command.
*/
Expand All @@ -62,7 +47,7 @@ public:
*
* This only returns the correct value after parseCmdline() has run.
*/
Path getCommandBaseDir() const;
virtual Path getCommandBaseDir() const;

protected:

Expand Down
14 changes: 13 additions & 1 deletion src/libutil/args/root.hh
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,26 @@ struct Completions final : AddCompletions
*/
class RootArgs : virtual public Args
{
/**
* @brief The command's "working directory", but only set when top level.
*
* Use getCommandBaseDir() to get the directory regardless of whether this
* is a top-level command or subcommand.
*
* @see getCommandBaseDir()
*/
Path commandBaseDir = ".";

public:
/** Parse the command line, throwing a UsageError if something goes
* wrong.
*/
void parseCmdline(const Strings & cmdline);
void parseCmdline(const Strings & cmdline, bool allowShebang = false);

std::shared_ptr<Completions> completions;

Path getCommandBaseDir() const override;

protected:

friend class Args;
Expand Down

0 comments on commit ffd414e

Please sign in to comment.