[Pitch] Merge local and global mirror configuration

Hi everyone!

I want to make a change to how Swift handles local and global mirror configuration. Currently, if a project contains a local configuration file, any global configuration is ignored. This is different from how package registry configuration works; for that, the local and global configurations are merged, with local configuration having higher precedence. I suggest that mirror configuration behave in the same way.

Introduction

SPM has support for both local (per-project) and global (per-user) mirror configuration. However, any global configuration is ignored when a local configuration is present. With this proposal, the configuration is instead merged, so that both local and global configuration can be used for the same project.

Motivation

Originally, mirrors could only be configured locally, per project, as described by the original proposal.

However, support for a global configuration file was added in a later release, mirroring the design of package registries, which has supported local and global configuration since the start.

Global configuration files for mirrors and package registries are especially useful in enterprise environments, where certain packages will always be accessed from a custom URL. The usefulness of global configuration is increased even further with the support for mirroring of binary targets.

However, the implementation of how the configuration for mirrors and packages registries is read differs. For package registries, if both local and global configuration is used, the result is a merged configuration, with the local configuration taking higher precedence in case of a conflict. For mirrors, on the other hand, the global configuration is ignored, and only the local configuration is used.

This causes issues in environments that want to have "global" mirror configuration, as well as "per-project" configuration. For example, you might have a global configuration with a lot of entries that is decided on by your organization, and want to temporarily use a local configuration to add or change one entry. Currently, you would need to either copy all of the global configuration to the local, or change the global one. The former is cumbersome and error-prone; the latter might not be an option since it would affect all other projects on the same machine, for that user.

Lastly, the current situation is somewhat confusing, with mirror and package registry configuration behaving differently. This is especially true when they are used in tandem, e.g. using a package registry for an SPM package, and a mirror for one of its binary targets.

Proposed solution

This proposal changes the behavior so that both local and global mirror configuration can be used, with local configuration taking precedence if both have entries for the same URL.

Example

Content of local configuration:

{
  "object" : [
    {
      "mirror": "https://internal.com/file1.json",
      "original": "https://example.com/file1.json"
    },
    {
      "mirror": "https://internal.com/file2-override.json",
      "original": "https://example.com/file2.json"
    }
  ],
  "version": 1
}

Content of global configuration:

{
  "object": [
    {
      "mirror": "https://internal.com/file2.json",
      "original": "https://example.com/file2.json"
    },
    {
      "mirror": "https://internal.com/file3.json",
      "original": "https://example.com/file3.json"
    }
  ],
  "version": 1

}

In this example, https://example.com/file1.json is mirrored to https://internal.com/file1.json, as it only exists in the local configuration.
https://example.com/file2.json exists in both the local and global configuration, and is mirrored to https://internal.com/file2-override.json, as the local configuration takes precedence.
https://example.com/file3.json exists only in the global configuration, and is mirrored to https://internal.com/file3.json.

Detailed design

Commands that read the mirror configuration are changed to use both the local and global configurations, combining them into a merged configuration. We say that entries from the local and global configuration conflict if they both have the same "original" URL. For entries that do not conflict, (i.e. the "original" URL only exists in either the local or global configuration), that entry is included in the merged configuration. For entries that do conflict, the one from the local configuration is used.
Note that two entries that have the same "mirror" URL do not conflict (unless they also have the same "original" URL).

This affects commands that download dependencies (e.g. swift package resolve), and commands for viewing the current mirror configuration (swift package config get-mirror).

In situations where only one of the configurations exists, or when explicitly using only the global configuration (e.g. swift package config get-mirror --global), the behavior is unchanged.

Security

This proposal has minimal impact on security. If building a project on a shared/untrusted machine, the global configuration could be seen as a way to inject malicious configuration. However, since the global configuration is only ignored if there is a non-empty local configuration, this change in itself does not introduce the potential issue. Prevention of such attacks should rather be done with checksums, as in Package.resolved and .binaryTarget.

Impact on existing packages

This proposal changes the behavior for any developer that currently uses both local and global configuration. It could break builds where a local configuration is explicitly used to ignore the content of a global configuration. However, this does not seem like an intended usage of local and global configuration - if this is a legitimate use-case it would be better to add some other mechanism to ignore the global configuration.

Alternatives considered

None

EDIT: Added "Detailed design"

3 Likes

This seems like clearly the right thing to do given how other config files work. cc @build-and-packaging-workgroup and @ecosystem-steering-group in case anyone has opinions on if this could be considered a bug fix due to a quirk of how the mirrors feature evolved, or if anyone thinks there's a larger design discussion to be had here.

2 Likes

I have had discussions with some of the SwiftPM contributors here, and there is a real need to redo how we manage dependencies and mirrors, along with the replace-scm-with-registry flag with a unified dependency override mechanism, probably as part of the Workspace. But that's down the road. We probably would want a better global versus local mechanism so this does seem like a way forward in the short term.

2 Likes

I'm curious, what are some of the changes/improvements you are discussing? As an end user, I think it works mostly fine, apart from some quirks like the one addressed here. You said something similar in the review for SE-0535 (SE-0535: Add CLI for editing global mirrors configuration - #3 by dschaefer2), and so I don't want to keep pushing the current implementation beyond what's reasonable.

One thing that's come up a number of times is the need for a "workspace" file, similar to xcworkspace in Xcode which lets you bundle a number of root packages into a single file that you can open up. There's even a hidden option in the SwiftPM CLI to use the same files. When you load multiple root packages, all dependencies on those packages get overridden to use the one listed in the roots.

That is another way to do mirrors. Also the --replace-scm-with-registry flag lets you override dependencies to use packages from a package registry using a bunch of heuristics that don't always work. The mirrors file today only overrides URLs.

My hope is to consolidate these dependency override mechanisms into one. I'd like to see a portable workspace file format (JSON maybe?) that lists the root packages and then lists a mapping of package identities to the locations for those package to be used instead, either a local path, a new source control URL, a package registry id, and whatever we support in the future.

The workspace file would probably hold other info, like a set of workspace "settings", e.g. all the things that go into SwiftPM's Workspace and WorkspaceConfiguration objects and are settable on the command line today.

But, there are a few things we need to do first. We have been pretty bad at modelling package identity and are looking to clean that up with the Package URL standard that would help us formalize the dependency mapping. And then decide on the format of this file and how it works with IDEs and the command line. But that's going to take some time to get together.

2 Likes

Thanks, that's interesting to hear! Yeah, I guess there are a lot of different environments and use cases, each with their own requirements, and not an easy task to satisfy them all. I'm glad then that you're still accepting short-term improvements.