Fix `dependency_services apply` for packages with relative imports (#3374)

diff --git a/lib/src/command/dependency_services.dart b/lib/src/command/dependency_services.dart
index 45833d4..46c1b94 100644
--- a/lib/src/command/dependency_services.dart
+++ b/lib/src/command/dependency_services.dart
@@ -8,6 +8,7 @@
 import 'dart:io';
 
 import 'package:collection/collection.dart';
+import 'package:path/path.dart';
 import 'package:pub_semver/pub_semver.dart';
 import 'package:yaml/yaml.dart';
 import 'package:yaml_edit/yaml_edit.dart';
@@ -367,7 +368,11 @@
 
     final updatedLockfile = lockFileEditor == null
         ? null
-        : LockFile.parse(lockFileEditor.toString(), cache.sources);
+        : LockFile.parse(
+            lockFileEditor.toString(),
+            cache.sources,
+            filePath: entrypoint.lockFilePath,
+          );
     await log.warningsOnlyUnlessTerminal(
       () async {
         final updatedPubspec = pubspecEditor.toString();
@@ -381,7 +386,8 @@
         final solveResult = await resolveVersions(
           SolveType.get,
           cache,
-          Package.inMemory(Pubspec.parse(updatedPubspec, cache.sources)),
+          Package.inMemory(Pubspec.parse(updatedPubspec, cache.sources,
+              location: toUri(entrypoint.pubspecPath))),
           lockFile: updatedLockfile,
         );
         if (pubspecEditor.edits.isNotEmpty) {
diff --git a/lib/src/lock_file.dart b/lib/src/lock_file.dart
index 0efc3af..0950de1 100644
--- a/lib/src/lock_file.dart
+++ b/lib/src/lock_file.dart
@@ -81,8 +81,12 @@
   }
 
   /// Parses a lockfile whose text is [contents].
-  factory LockFile.parse(String contents, SourceRegistry sources) {
-    return LockFile._parse(null, contents, sources);
+  ///
+  /// If [filePath] is given, path-dependencies will be interpreted relative to
+  /// that.
+  factory LockFile.parse(String contents, SourceRegistry sources,
+      {String? filePath}) {
+    return LockFile._parse(filePath, contents, sources);
   }
 
   /// Parses the lockfile whose text is [contents].
diff --git a/test/dependency_services/dependency_services_test.dart b/test/dependency_services/dependency_services_test.dart
index ed21a19..3830e27 100644
--- a/test/dependency_services/dependency_services_test.dart
+++ b/test/dependency_services/dependency_services_test.dart
@@ -250,6 +250,28 @@
       );
     });
   });
+  testWithGolden('Relative paths are allowed', (context) async {
+    // We cannot update path-dependencies, but they should be allowed.
+    final server = await servePackages();
+    server.serve('foo', '1.0.0');
+    await d.dir('bar', [d.libPubspec('bar', '1.0.0')]).create();
+
+    await d.appDir({
+      'foo': '^1.0.0',
+      'bar': {'path': '../bar'}
+    }).create();
+    await pubGet();
+    server.serve('foo', '2.0.0');
+    await listReportApply(context, [
+      _PackageVersion('foo', Version.parse('2.0.0'),
+          constraint: VersionConstraint.parse('^2.0.0')),
+    ], reportAssertions: (report) {
+      expect(
+        findChangeVersion(report, 'multiBreaking', 'foo'),
+        '2.0.0',
+      );
+    });
+  });
 }
 
 dynamic findChangeVersion(dynamic json, String updateType, String name) {
diff --git a/test/testdata/goldens/dependency_services/dependency_services_test/Relative paths are allowed.txt b/test/testdata/goldens/dependency_services/dependency_services_test/Relative paths are allowed.txt
new file mode 100644
index 0000000..bbe5052
--- /dev/null
+++ b/test/testdata/goldens/dependency_services/dependency_services_test/Relative paths are allowed.txt
@@ -0,0 +1,126 @@
+# GENERATED BY: test/dependency_services/dependency_services_test.dart
+
+$ cat pubspec.yaml
+{"name":"myapp","environment":{"sdk":">=0.1.2 <1.0.0"},"dependencies":{"foo":"^1.0.0","bar":{"path":"../bar"}}}
+$ cat pubspec.lock
+# Generated by pub
+# See https://dart.dev/tools/pub/glossary#lockfile
+packages:
+  bar:
+    dependency: "direct main"
+    description:
+      path: "../bar"
+      relative: true
+    source: path
+    version: "1.0.0"
+  foo:
+    dependency: "direct main"
+    description:
+      name: foo
+      url: "http://localhost:$PORT"
+    source: hosted
+    version: "1.0.0"
+sdks:
+  dart: ">=0.1.2 <1.0.0"
+-------------------------------- END OF OUTPUT ---------------------------------
+
+## Section list
+$ dependency_services list
+{
+  "dependencies": [
+    {
+      "name": "bar",
+      "version": "1.0.0",
+      "kind": "direct",
+      "constraint": "any"
+    },
+    {
+      "name": "foo",
+      "version": "1.0.0",
+      "kind": "direct",
+      "constraint": "^1.0.0"
+    }
+  ]
+}
+
+-------------------------------- END OF OUTPUT ---------------------------------
+
+## Section report
+$ dependency_services report
+{
+  "dependencies": [
+    {
+      "name": "bar",
+      "version": "1.0.0",
+      "kind": "direct",
+      "latest": "1.0.0",
+      "constraint": "any",
+      "compatible": [],
+      "singleBreaking": [],
+      "multiBreaking": []
+    },
+    {
+      "name": "foo",
+      "version": "1.0.0",
+      "kind": "direct",
+      "latest": "2.0.0",
+      "constraint": "^1.0.0",
+      "compatible": [],
+      "singleBreaking": [
+        {
+          "name": "foo",
+          "version": "2.0.0",
+          "kind": "direct",
+          "constraintBumped": "^2.0.0",
+          "constraintWidened": ">=1.0.0 <3.0.0",
+          "constraintBumpedIfNeeded": "^2.0.0",
+          "previousVersion": "1.0.0",
+          "previousConstraint": "^1.0.0"
+        }
+      ],
+      "multiBreaking": [
+        {
+          "name": "foo",
+          "version": "2.0.0",
+          "kind": "direct",
+          "constraintBumped": "^2.0.0",
+          "constraintWidened": ">=1.0.0 <3.0.0",
+          "constraintBumpedIfNeeded": "^2.0.0",
+          "previousVersion": "1.0.0",
+          "previousConstraint": "^1.0.0"
+        }
+      ]
+    }
+  ]
+}
+
+-------------------------------- END OF OUTPUT ---------------------------------
+
+## Section apply
+$ echo '{"dependencyChanges":[{"name":"foo","version":"2.0.0","constraint":"^2.0.0"}]}' | dependency_services apply
+{"dependencies":[]}
+
+-------------------------------- END OF OUTPUT ---------------------------------
+
+$ cat pubspec.yaml
+{"name":"myapp","environment":{"sdk":">=0.1.2 <1.0.0"},"dependencies":{"foo":^2.0.0,"bar":{"path":"../bar"}}}
+$ cat pubspec.lock
+# Generated by pub
+# See https://dart.dev/tools/pub/glossary#lockfile
+packages:
+  bar:
+    dependency: "direct main"
+    description:
+      path: "../bar"
+      relative: true
+    source: path
+    version: "1.0.0"
+  foo:
+    dependency: "direct main"
+    description:
+      name: foo
+      url: "http://localhost:$PORT"
+    source: hosted
+    version: "2.0.0"
+sdks:
+  dart: ">=0.1.2 <1.0.0"