@@ -46,9 +46,18 @@ extension on Dependency {
4646 final that = this ;
4747 if (that is HostedDependency ) {
4848 if (that.hosted != null ) {
49+ String ? safeName;
50+ try {
51+ safeName = that.hosted! .name;
52+
53+ // `that.hosted!.name` could throw an error if `_nameOfPackage` is null in the getter.
54+ // We need to safely handle this scenario because we can't guarantee that the value is not null.
55+ // ignore: avoid_catching_errors
56+ } on Error catch (_) {}
57+
4958 return {
5059 'hosted' : {
51- 'name' : that.hosted ! .name ,
60+ if (safeName != null ) 'name' : safeName ,
5261 'url' : that.hosted! .url.toString (),
5362 },
5463 'version' : that.version.toString (),
@@ -2256,6 +2265,97 @@ dependencies:
22562265 version: "1.0.0"
22572266''' );
22582267 });
2268+ group (
2269+ 'Support hosted project with custom source' ,
2270+ () {
2271+ test (
2272+ 'If a dependency comes from a custom hosted source, the generated pubspec.yaml should contain the hosted source' ,
2273+ () async {
2274+ final workingDir = await createSimpleWorkspace ([
2275+ Pubspec (
2276+ 'plugin1' ,
2277+ dependencies: {
2278+ 'custom_lint_builder' : HostedDependency (),
2279+ },
2280+ ),
2281+ Pubspec (
2282+ 'a' ,
2283+ devDependencies: {
2284+ 'plugin1' : HostedDependency (
2285+ hosted: HostedDetails (
2286+ 'plugin1' ,
2287+ Uri .parse ('https://custom.com' ),
2288+ ),
2289+ version: Version (1 , 0 , 0 ),
2290+ ),
2291+ },
2292+ ),
2293+ ]);
2294+
2295+ final workspace = await fromContextRootsFromPaths (
2296+ ['a' ],
2297+ workingDirectory: workingDir,
2298+ );
2299+
2300+ expect (workspace.computePubspec (), '''
2301+ name: custom_lint_client
2302+ description: A client for custom_lint
2303+ version: 0.0.1
2304+ publish_to: 'none'
2305+
2306+ dependencies:
2307+ plugin1:
2308+ hosted:
2309+ name: plugin1
2310+ url: https://custom.com
2311+ version: "1.0.0"
2312+ ''' );
2313+ });
2314+ test (
2315+ 'Hosted withouth name should still work' ,
2316+ () async {
2317+ final workingDir = await createSimpleWorkspace ([
2318+ Pubspec (
2319+ 'plugin1' ,
2320+ dependencies: {
2321+ 'custom_lint_builder' : HostedDependency (),
2322+ },
2323+ ),
2324+ Pubspec (
2325+ 'a' ,
2326+ devDependencies: {
2327+ 'plugin1' : HostedDependency (
2328+ hosted: HostedDetails (
2329+ null ,
2330+ Uri .parse ('https://custom.com' ),
2331+ ),
2332+ version: Version (1 , 0 , 0 ),
2333+ ),
2334+ },
2335+ ),
2336+ ]);
2337+
2338+ final workspace = await fromContextRootsFromPaths (
2339+ ['a' ],
2340+ workingDirectory: workingDir,
2341+ );
2342+
2343+ expect (workspace.computePubspec (), '''
2344+ name: custom_lint_client
2345+ description: A client for custom_lint
2346+ version: 0.0.1
2347+ publish_to: 'none'
2348+
2349+ dependencies:
2350+ plugin1:
2351+ hosted:
2352+ url: https://custom.com
2353+ version: "1.0.0"
2354+ ''' );
2355+ },
2356+ );
2357+ },
2358+ );
22592359 });
22602360
22612361 group (CustomLintWorkspace .fromPaths, () {
0 commit comments