1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-02 13:31:48 +02:00

Make 'nix edit' etc. work again

This commit is contained in:
Eelco Dolstra 2022-07-26 17:06:45 +02:00
parent 1790698a74
commit bb4d35dcca
15 changed files with 108 additions and 82 deletions

View file

@ -106,7 +106,7 @@ std::pair<Value *, PosIdx> findAlongAttrPath(EvalState & state, const std::strin
}
std::pair<std::string, uint32_t> findPackageFilename(EvalState & state, Value & v, std::string what)
std::pair<SourcePath, uint32_t> findPackageFilename(EvalState & state, Value & v, std::string what)
{
Value * v2;
try {
@ -120,19 +120,41 @@ std::pair<std::string, uint32_t> findPackageFilename(EvalState & state, Value &
// toString + parsing?
auto pos = state.forceString(*v2);
auto colon = pos.rfind(':');
if (colon == std::string::npos)
throw ParseError("cannot parse meta.position attribute '%s'", pos);
auto fail = [pos]() {
throw ParseError("cannot parse 'meta.position' attribute '%s'", pos);
};
std::string filename(pos, 0, colon);
unsigned int lineno;
try {
lineno = std::stoi(std::string(pos, colon + 1, std::string::npos));
std::string_view prefix = "/virtual/";
if (!hasPrefix(pos, prefix)) fail();
pos = pos.substr(prefix.size());
auto slash = pos.find('/');
if (slash == std::string::npos) fail();
size_t number = std::stoi(std::string(pos, 0, slash));
pos = pos.substr(slash);
std::shared_ptr<InputAccessor> accessor;
for (auto & i : state.inputAccessors)
if (i.second->number == number) {
accessor = i.second;
break;
}
if (!accessor) fail();
auto colon = pos.rfind(':');
if (colon == std::string::npos) fail();
std::string filename(pos, 0, colon);
auto lineno = std::stoi(std::string(pos, colon + 1, std::string::npos));
return {SourcePath{ref(accessor), CanonPath(filename)}, lineno};
} catch (std::invalid_argument & e) {
throw ParseError("cannot parse line number '%s'", pos);
fail();
abort();
}
return { std::move(filename), lineno };
}

View file

@ -17,7 +17,7 @@ std::pair<Value *, PosIdx> findAlongAttrPath(
Value & vIn);
/* Heuristic to find the filename and lineno or a nix value. */
std::pair<std::string, uint32_t> findPackageFilename(EvalState & state, Value & v, std::string what);
std::pair<SourcePath, uint32_t> findPackageFilename(EvalState & state, Value & v, std::string what);
std::vector<Symbol> parseAttrPath(EvalState & state, std::string_view s);

View file

@ -1097,7 +1097,7 @@ void EvalState::mkPos(Value & v, PosIdx p)
auto pos = positions[p];
if (auto path = std::get_if<SourcePath>(&pos.origin)) {
auto attrs = buildBindings(3);
attrs.alloc(sFile).mkString(path->path.abs());
attrs.alloc(sFile).mkString(fmt("/virtual/%d%s", path->accessor->number, path->path.abs()));
attrs.alloc(sLine).mkInt(pos.line);
attrs.alloc(sColumn).mkInt(pos.column);
v.mkAttrs(attrs);

View file

@ -431,10 +431,10 @@ LockedFlake lockFlake(
flakerefs relative to the parent flake. */
auto getInputFlake = [&]()
{
if (input.ref->input.isRelative()) {
if (auto relativePath = input.ref->input.isRelative()) {
SourcePath inputSourcePath {
overridenSourcePath.accessor,
CanonPath(*input.ref->input.getSourcePath(), *overridenSourcePath.path.parent())
*overridenSourcePath.path.parent() + *relativePath
};
return readFlake(state, *input.ref, *input.ref, *input.ref, inputSourcePath, inputPath);
} else
@ -621,7 +621,7 @@ LockedFlake lockFlake(
auto diff = LockFile::diff(oldLockFile, newLockFile);
if (lockFlags.writeLockFile) {
if (auto sourcePath = topRef.input.getSourcePath()) {
if (auto sourcePath = topRef.input.getAccessor(state.store).first->root().getPhysicalPath()) {
if (auto unlockedInput = newLockFile.isUnlocked()) {
if (fetchSettings.warnDirty)
warn("will not write lock file of flake '%s' because it has an unlocked input ('%s')", topRef, *unlockedInput);
@ -629,11 +629,13 @@ LockedFlake lockFlake(
if (!lockFlags.updateLockFile)
throw Error("flake '%s' requires lock file changes but they're not allowed due to '--no-update-lock-file'", topRef);
auto relPath = (topRef.subdir == "" ? "" : topRef.subdir + "/") + "flake.lock";
CanonPath flakeDir(*sourcePath);
auto path = *sourcePath + "/" + relPath;
auto relPath = flakeDir + "flake.lock";
bool lockFileExists = pathExists(path);
auto path = flakeDir + "flake.lock";
bool lockFileExists = pathExists(path.abs());
if (lockFileExists) {
auto s = chomp(diff);
@ -644,7 +646,7 @@ LockedFlake lockFlake(
} else
warn("creating lock file '%s'", path);
newLockFile.write(path);
newLockFile.write(path.abs());
std::optional<std::string> commitMessage = std::nullopt;
if (lockFlags.commitLockFile) {