@@ -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 (),
@@ -2255,6 +2264,97 @@ dependencies:
22552264 version: "1.0.0"
22562265''' );
22572266 });
2267+ group (
2268+ 'Support hosted project with custom source' ,
2269+ () {
2270+ test (
2271+ 'If a dependency comes from a custom hosted source, the generated pubspec.yaml should contain the hosted source' ,
2272+ () async {
2273+ final workingDir = await createSimpleWorkspace ([
2274+ Pubspec (
2275+ 'plugin1' ,
2276+ dependencies: {
2277+ 'custom_lint_builder' : HostedDependency (),
2278+ },
2279+ ),
2280+ Pubspec (
2281+ 'a' ,
2282+ devDependencies: {
2283+ 'plugin1' : HostedDependency (
2284+ hosted: HostedDetails (
2285+ 'plugin1' ,
2286+ Uri .parse ('https://custom.com' ),
2287+ ),
2288+ version: Version (1 , 0 , 0 ),
2289+ ),
2290+ },
2291+ ),
2292+ ]);
2293+
2294+ final workspace = await fromContextRootsFromPaths (
2295+ ['a' ],
2296+ workingDirectory: workingDir,
2297+ );
2298+
2299+ expect (workspace.computePubspec (), '''
2300+ name: custom_lint_client
2301+ description: A client for custom_lint
2302+ version: 0.0.1
2303+ publish_to: 'none'
2304+
2305+ dependencies:
2306+ plugin1:
2307+ hosted:
2308+ name: plugin1
2309+ url: https://custom.com
2310+ version: "1.0.0"
2311+ ''' );
2312+ });
2313+ test (
2314+ 'Hosted withouth name should still work' ,
2315+ () async {
2316+ final workingDir = await createSimpleWorkspace ([
2317+ Pubspec (
2318+ 'plugin1' ,
2319+ dependencies: {
2320+ 'custom_lint_builder' : HostedDependency (),
2321+ },
2322+ ),
2323+ Pubspec (
2324+ 'a' ,
2325+ devDependencies: {
2326+ 'plugin1' : HostedDependency (
2327+ hosted: HostedDetails (
2328+ null ,
2329+ Uri .parse ('https://custom.com' ),
2330+ ),
2331+ version: Version (1 , 0 , 0 ),
2332+ ),
2333+ },
2334+ ),
2335+ ]);
2336+
2337+ final workspace = await fromContextRootsFromPaths (
2338+ ['a' ],
2339+ workingDirectory: workingDir,
2340+ );
2341+
2342+ expect (workspace.computePubspec (), '''
2343+ name: custom_lint_client
2344+ description: A client for custom_lint
2345+ version: 0.0.1
2346+ publish_to: 'none'
2347+
2348+ dependencies:
2349+ plugin1:
2350+ hosted:
2351+ url: https://custom.com
2352+ version: "1.0.0"
2353+ ''' );
2354+ },
2355+ );
2356+ },
2357+ );
22582358 });
22592359
22602360 group (CustomLintWorkspace .fromPaths, () {
0 commit comments