Handle pubspec_overrides.yaml in `add` and `upgrade --major-versions` (#3920)

diff --git a/lib/src/command/add.dart b/lib/src/command/add.dart
index 8c38636..06a0260 100644
--- a/lib/src/command/add.dart
+++ b/lib/src/command/add.dart
@@ -2,6 +2,8 @@
 // for details. All rights reserved. Use of this source code is governed by a
 // BSD-style license that can be found in the LICENSE file.
 
+import 'dart:io';
+
 import 'package:args/args.dart';
 import 'package:collection/collection.dart';
 import 'package:path/path.dart' as p;
@@ -245,6 +247,15 @@
       writeTextFile(entrypoint.pubspecPath, newPubspecText);
     }
 
+    String? overridesFileContents;
+    final overridesPath =
+        p.join(entrypoint.rootDir, Pubspec.pubspecOverridesFilename);
+    try {
+      overridesFileContents = readTextFile(overridesPath);
+    } on IOException {
+      overridesFileContents = null;
+    }
+
     /// Even if it is a dry run, run `acquireDependencies` so that the user
     /// gets a report on the other packages that might change version due
     /// to this new dependency.
@@ -254,6 +265,8 @@
             newPubspecText,
             cache.sources,
             location: Uri.parse(entrypoint.pubspecPath),
+            overridesFileContents: overridesFileContents,
+            overridesLocation: Uri.file(overridesPath),
           ),
         )
         .acquireDependencies(
diff --git a/lib/src/command/upgrade.dart b/lib/src/command/upgrade.dart
index 1cfcb59..6c1dcda 100644
--- a/lib/src/command/upgrade.dart
+++ b/lib/src/command/upgrade.dart
@@ -3,7 +3,9 @@
 // BSD-style license that can be found in the LICENSE file.
 
 import 'dart:async';
+import 'dart:io';
 
+import 'package:path/path.dart' as p;
 import 'package:pub_semver/pub_semver.dart';
 import 'package:yaml_edit/yaml_edit.dart';
 
@@ -247,12 +249,23 @@
       }
     }
 
+    String? overridesFileContents;
+    final overridesPath =
+        p.join(entrypoint.rootDir, Pubspec.pubspecOverridesFilename);
+    try {
+      overridesFileContents = readTextFile(overridesPath);
+    } on IOException {
+      overridesFileContents = null;
+    }
+
     await entrypoint
         .withPubspec(
           Pubspec.parse(
             newPubspecText,
             cache.sources,
             location: Uri.parse(entrypoint.pubspecPath),
+            overridesFileContents: overridesFileContents,
+            overridesLocation: Uri.file(overridesPath),
           ),
         )
         .acquireDependencies(
diff --git a/test/add/common/add_test.dart b/test/add/common/add_test.dart
index f4a611b..8e958af 100644
--- a/test/add/common/add_test.dart
+++ b/test/add/common/add_test.dart
@@ -1081,4 +1081,26 @@
       )
     ]).validate();
   });
+
+  test('should take pubspec_overrides.yaml into account', () async {
+    final server = await servePackages();
+    server.serve('foo', '1.0.0');
+    await d.dir('bar', [d.libPubspec('bar', '1.0.0')]).create();
+    await d.appDir(
+      dependencies: {
+        'bar': '^1.0.0',
+      },
+    ).create();
+    await d.dir(appPath, [
+      d.pubspecOverrides({
+        'dependency_overrides': {
+          'bar': {'path': '../bar'}
+        }
+      })
+    ]).create();
+
+    await pubGet();
+
+    await pubAdd(args: ['foo'], output: contains('+ foo 1.0.0'));
+  });
 }
diff --git a/test/upgrade/upgrade_major_versions_test.dart b/test/upgrade/upgrade_major_versions_test.dart
index 40c23ea..691b221 100644
--- a/test/upgrade/upgrade_major_versions_test.dart
+++ b/test/upgrade/upgrade_major_versions_test.dart
@@ -277,5 +277,35 @@
         d.packageConfigEntry(name: 'bar', version: '4.0.0'),
       ]).validate();
     });
+
+    test('should take pubspec_overrides.yaml into account', () async {
+      await servePackages()
+        ..serve('foo', '1.0.0')
+        ..serve('foo', '2.0.0');
+      await d.dir('bar', [d.libPubspec('bar', '1.0.0')]).create();
+      await d.appDir(
+        dependencies: {
+          'foo': '^1.0.0',
+          'bar': '^1.0.0',
+        },
+      ).create();
+      await d.dir(appPath, [
+        d.pubspecOverrides({
+          'dependency_overrides': {
+            'bar': {'path': '../bar'}
+          }
+        })
+      ]).create();
+
+      await pubGet();
+
+      await pubUpgrade(
+        args: ['--major-versions'],
+        output: allOf([
+          contains('Changed 1 constraint in pubspec.yaml:'),
+          contains('foo: ^1.0.0 -> ^2.0.0'),
+        ]),
+      );
+    });
   });
 }