libcmd: asyncify lookupFileArg
Change-Id: Ied47f5f96eeb8324c500335fa8f5388d10bed54c
This commit is contained in:
@@ -254,10 +254,10 @@ static void main_nix_build(AsyncIoRoot & aio, std::string programName, Strings a
|
||||
/* If we're in a #! script, interpret filenames
|
||||
relative to the script. */
|
||||
exprs.push_back(evaluator->parseExprFromFile(
|
||||
evaluator->paths.resolveExprPath(lookupFileArg(
|
||||
evaluator->paths.resolveExprPath(aio.blockOn(lookupFileArg(
|
||||
*evaluator,
|
||||
inShebang && !packages ? absPath(i, absPath(dirOf(script))) : i
|
||||
))
|
||||
)))
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1547,7 +1547,7 @@ static int main_nix_env(AsyncIoRoot & aio, std::string programName, Strings argv
|
||||
|
||||
globals.instSource.nixExprPath = std::make_shared<SourcePath>(
|
||||
file != ""
|
||||
? lookupFileArg(*globals.state, file)
|
||||
? aio.blockOn(lookupFileArg(*globals.state, file))
|
||||
: CanonPath(nixExprPath));
|
||||
|
||||
globals.instSource.autoArgs = myArgs.getAutoArgs(*globals.state);
|
||||
|
||||
@@ -183,7 +183,9 @@ static int main_nix_instantiate(AsyncIoRoot & aio, std::string programName, Stri
|
||||
for (auto & i : files) {
|
||||
Expr & e = fromArgs
|
||||
? evaluator->parseExprFromString(i, CanonPath::fromCwd())
|
||||
: evaluator->parseExprFromFile(evaluator->paths.resolveExprPath(lookupFileArg(*evaluator, i)));
|
||||
: evaluator->parseExprFromFile(
|
||||
evaluator->paths.resolveExprPath(aio.blockOn(lookupFileArg(*evaluator, i)))
|
||||
);
|
||||
processExpr(*state, attrPaths, parseOnly, strict, autoArgs,
|
||||
evalOnly, outputKind, xmlOutputSourceLocation, e);
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "lix/libexpr/flake/flakeref.hh"
|
||||
#include "lix/libstore/store-api.hh"
|
||||
#include "lix/libcmd/command.hh"
|
||||
#include "lix/libutil/async.hh"
|
||||
|
||||
#include <regex>
|
||||
|
||||
@@ -187,8 +188,8 @@ Bindings * MixEvalArgs::getAutoArgs(Evaluator & state)
|
||||
return res.finish();
|
||||
}
|
||||
|
||||
SourcePath lookupFileArg(Evaluator & state, std::string_view fileArg)
|
||||
{
|
||||
kj::Promise<Result<SourcePath>> lookupFileArg(Evaluator & state, std::string_view fileArg)
|
||||
try {
|
||||
if (EvalSettings::isPseudoUrl(fileArg)) {
|
||||
auto const url = EvalSettings::resolvePseudoUrl(fileArg);
|
||||
auto const downloaded = fetchers::downloadTarball(
|
||||
@@ -198,21 +199,22 @@ SourcePath lookupFileArg(Evaluator & state, std::string_view fileArg)
|
||||
/* locked */ false
|
||||
);
|
||||
StorePath const storePath = downloaded.tree.storePath;
|
||||
return CanonPath(state.store->toRealPath(storePath));
|
||||
co_return CanonPath(state.store->toRealPath(storePath));
|
||||
} else if (fileArg.starts_with("flake:")) {
|
||||
experimentalFeatureSettings.require(Xp::Flakes);
|
||||
static constexpr size_t FLAKE_LEN = std::string_view("flake:").size();
|
||||
auto flakeRef = parseFlakeRef(std::string(fileArg.substr(FLAKE_LEN)), {}, true, false);
|
||||
auto storePath =
|
||||
RUN_ASYNC_IN_NEW_THREAD(flakeRef.resolve(state.store).fetchTree(state.store))
|
||||
.first.storePath;
|
||||
return CanonPath(state.store->toRealPath(storePath));
|
||||
TRY_AWAIT(flakeRef.resolve(state.store).fetchTree(state.store)).first.storePath;
|
||||
co_return CanonPath(state.store->toRealPath(storePath));
|
||||
} else if (fileArg.size() > 2 && fileArg.at(0) == '<' && fileArg.at(fileArg.size() - 1) == '>') {
|
||||
Path p(fileArg.substr(1, fileArg.size() - 2));
|
||||
return RUN_ASYNC_IN_NEW_THREAD(state.paths.findFile(p));
|
||||
co_return TRY_AWAIT(state.paths.findFile(p));
|
||||
} else {
|
||||
return CanonPath::fromCwd(fileArg);
|
||||
co_return CanonPath::fromCwd(fileArg);
|
||||
}
|
||||
} catch (...) {
|
||||
co_return result::current_exception();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -49,6 +49,6 @@ private:
|
||||
*
|
||||
* @exception nix::ThrownError for failed search path lookup. Probably others.
|
||||
*/
|
||||
SourcePath lookupFileArg(Evaluator & state, std::string_view fileArg);
|
||||
kj::Promise<Result<SourcePath>> lookupFileArg(Evaluator & state, std::string_view fileArg);
|
||||
|
||||
}
|
||||
|
||||
@@ -217,7 +217,7 @@ void SourceExprCommand::completeInstallable(EvalState & state, AddCompletions &
|
||||
auto evaluator = getEvaluator();
|
||||
|
||||
Expr & e = evaluator->parseExprFromFile(
|
||||
state.ctx.paths.resolveExprPath(lookupFileArg(*evaluator, *file))
|
||||
state.ctx.paths.resolveExprPath(state.aio.blockOn(lookupFileArg(*evaluator, *file)))
|
||||
);
|
||||
|
||||
Value root;
|
||||
@@ -454,7 +454,7 @@ Installables SourceExprCommand::parseInstallables(
|
||||
state.eval(e, *vFile);
|
||||
}
|
||||
else if (file)
|
||||
state.evalFile(lookupFileArg(*evaluator, *file), *vFile);
|
||||
state.evalFile(state.aio.blockOn(lookupFileArg(*evaluator, *file)), *vFile);
|
||||
else {
|
||||
auto & e = evaluator->parseExprFromString(*expr, CanonPath::fromCwd());
|
||||
state.eval(e, *vFile);
|
||||
|
||||
+1
-1
@@ -895,7 +895,7 @@ void NixRepl::loadFile(const Path & path)
|
||||
loadedFiles.remove(path);
|
||||
loadedFiles.push_back(path);
|
||||
Value v, v2;
|
||||
state.evalFile(lookupFileArg(evaluator, path), v);
|
||||
state.evalFile(state.aio.blockOn(lookupFileArg(evaluator, path)), v);
|
||||
state.autoCallFunction(*autoArgs, v, v2);
|
||||
addAttrsToScope(v2);
|
||||
}
|
||||
|
||||
+1
-1
@@ -203,7 +203,7 @@ static int main_nix_prefetch_url(AsyncIoRoot & aio, std::string programName, Str
|
||||
Value vRoot;
|
||||
state->evalFile(
|
||||
evaluator->paths.resolveExprPath(
|
||||
lookupFileArg(*evaluator, args.empty() ? "." : args[0])),
|
||||
aio.blockOn(lookupFileArg(*evaluator, args.empty() ? "." : args[0]))),
|
||||
vRoot);
|
||||
Value & v(*findAlongAttrPath(*state, attrPath, autoArgs, vRoot).first);
|
||||
state->forceAttrs(v, noPos, "while evaluating the source attribute to prefetch");
|
||||
|
||||
@@ -53,7 +53,7 @@ static nix::Value *releaseExprTopLevelValue(nix::EvalState &state,
|
||||
nix::CanonPath::fromCwd());
|
||||
state.eval(e, vTop);
|
||||
} else {
|
||||
state.evalFile(nix::lookupFileArg(state.ctx, args.releaseExpr), vTop);
|
||||
state.evalFile(state.aio.blockOn(nix::lookupFileArg(state.ctx, args.releaseExpr)), vTop);
|
||||
}
|
||||
|
||||
auto vRoot = state.ctx.mem.allocValue();
|
||||
|
||||
@@ -35,22 +35,25 @@ TEST(Arguments, lookupFileArg) {
|
||||
auto store = openStore("dummy://");
|
||||
auto state = std::make_shared<Evaluator>(aio, searchPath, store, store);
|
||||
|
||||
SourcePath const foundUnitData = lookupFileArg(*state, "<example>");
|
||||
SourcePath const foundUnitData = aio.blockOn(lookupFileArg(*state, "<example>"));
|
||||
EXPECT_EQ(foundUnitData.canonical(), canonDataPath);
|
||||
|
||||
// lookupFileArg should not resolve <search paths> if anything else is before or after it.
|
||||
SourcePath const yepEvenSpaces = lookupFileArg(*state, " <example>");
|
||||
SourcePath const yepEvenSpaces = aio.blockOn(lookupFileArg(*state, " <example>"));
|
||||
EXPECT_EQ(yepEvenSpaces.canonical(), CanonPath::fromCwd(" <example>"));
|
||||
EXPECT_EQ(lookupFileArg(*state, "<example>/nixos").canonical(), CanonPath::fromCwd("<example>/nixos"));
|
||||
EXPECT_EQ(
|
||||
aio.blockOn(lookupFileArg(*state, "<example>/nixos")).canonical(),
|
||||
CanonPath::fromCwd("<example>/nixos")
|
||||
);
|
||||
|
||||
try {
|
||||
lookupFileArg(*state, INVALID_CHANNEL);
|
||||
aio.blockOn(lookupFileArg(*state, INVALID_CHANNEL));
|
||||
} catch (FileTransferError const & ex) {
|
||||
std::string_view const msg(ex.what());
|
||||
EXPECT_NE(msg.find(CHANNEL_URL), msg.npos);
|
||||
}
|
||||
|
||||
SourcePath const normalFile = lookupFileArg(*state, unitDataPath);
|
||||
SourcePath const normalFile = aio.blockOn(lookupFileArg(*state, unitDataPath));
|
||||
EXPECT_EQ(normalFile.canonical(), canonDataPath);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user