# \[Pre-RFC\] \[idea\] Cratespaces (crates as namespace, take 2... or 3?)

**URL:** <https://internals.rust-lang.org/t/pre-rfc-idea-cratespaces-crates-as-namespace-take-2-or-3/11320>\
**Category:** cargo\
**Created:** [November 18, 2019, 7:06pm UTC](https://internals.rust-lang.org/t/pre-rfc-idea-cratespaces-crates-as-namespace-take-2-or-3/11320 "2019-11-18T19:06:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![samsieber](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/samsieber/32/4671_2.png) [@samsieber](https://internals.rust-lang.org/u/samsieber)\
**Post date:** [November 18, 2019, 7:06pm UTC](https://internals.rust-lang.org/t/pre-rfc-idea-cratespaces-crates-as-namespace-take-2-or-3/11320/1 "2019-11-18T19:06:40Z")

</div>

This is a slightly different take on the crates and namespaces suggestions made previously. Previous RFC's and discussions are linked at the end.

# Pre-RFC: Crate-spaces (crates as namespaces)

**Motivation** : There's been a discussion around mono-crates versus many small crates for project like tokio.

A group of small crates provides a number of benifits - granular compilation, independent versioning allowing for more flexible iteration, and an easy way to control how much code is compiled, compared to what is used.

A mono crate can control compilation through features, is easier to audit from an author perspective (bigger crates == fewer authors) and is easy to ensure all the bits work together

This proposal seeks to combine the best of both worlds.

**Purpose** : Allow for grouping crates into a cohesive whole, allowing for fine grained compilation while preserving crate metadata transparency, for ease of auditing and discovery.

**Considerations** :

- Backwards compatibility is pretty important - we don't want to arbitrarily cut off older software as
- Not adding another sigle in code for using namespaced crates

## Summary

Each top-level crate may act as a namespace. This doesn't prevent the crate from being used as a standalone crate. It might even be empty.

Additional crates (subcrates) may be published as children crates by the author(s) of the crate-space. These children crates are not directly accessible as top level crates (and therefore unaccessible to older rust versions). They may however be republished outside the cratespace by the author for backwards compatibility. There is no difference between using a child crate through the cratespace or as a standalone crate for versioning/interopability purposes. An external crate may be added to a single cratespace as a child crate, if the cratespace author and crate author agree. The primary use-case is for building a cratespace from existing crates.

A cratespace may import and publish an external crate as its own subcrate, with the caveat that is it marked as a voucher crate, not a child crate.

Subcrates are referenced as submodules of the main crate; they may be accessed via the path `crate::subcrate` in code. Subcrates may be versioned independently. A cratespace may publish a versioned version manifest, which assigns versions for all of its subcrates. Subcrates may be imported independent of the core crate space, but will still need to be referred to through the top.

### Examples

Serde could act a cratespace. Then serde-derive could be kept, but republished under serde as `derive` and accessed as `serde::derive` in code. The `serde_rustler` crate could be added as a voucher crate to the serde namespace as `rustler` and then referenced in code as `serde::rustler`, if so desired.

### Downsides

Maintaining backwards compatibility requires extra work - it's easy to be part of a cratespace or be backwards compatible, but being both requires effort.

In addition, this implementation generally adds complexity, and has a number of edges with undefined behavior. And despite care to not encourage things like user123/http, it is still possible. Multiple versioning schemes (independent vs manifest) are both desirable, but having both will probably lead to confusion.

### Alternatives and/or Possible Tweaks

- Domains as namespaces
- Don't implement namespaces
- Other namespace schemes
- For importing, use `_` instead of `::` and instead use crate\_subscrate
- For importing, drop the `cratespace::` prefix
- Require child crates to be published as separate crates first (to enforce backwards compatibility)
- Publish a crate with internal only dependencies (e.g. the dependencies aren't visible to the world); added per @CAD97

### Other Considerations

- Can we use tricks here to help with coherence (with voucher crates?) - how would that affect dependency resolution to avoid having two crates with the same impls.
- What happens when a subcrate is added - who has write/author permission for that crate?
- In this setup, it would be nice to allow a request for voucher crate status mechanism. It would probabl be safest for voucher status to only apply to the set of crate versions published by the approved authors, and new authors would require a new approval... right?
- Generally, voucher crates will need some serious consideration to avoid security issues, I think.

# Previous Discussions and RFC's on Namespacing:

> [@\[Pre-RFC\]: Packages as Namespaces](https://internals.rust-lang.org/t/pre-rfc-packages-as-namespaces/8628):
>
> There’s been a lot of chatter about adding namespaces to crates.io recently, so apologies if this idea has already been discussed. Summary Allow packages to be used as namespaces in a way that is backwards compatible with the existing flat namespace. Details The owners of a crate on crates.io can add sub-crates which are referred to by the name ‘toplevelcrate/subcrate’ in Cargo.toml files. For example, instead of publishing regex-syntax, the regex maintainers could have published regex/syntax.…

> [@\[Pre-RFC\] Domains as namespaces](https://internals.rust-lang.org/t/pre-rfc-domains-as-namespaces/8688):
>
> Summary Provide a namespacing facility which addresses the following requirements: Allow claiming a namespace exclusively, thereby restricting which crates can be published under this namespace. This allows addressing the squatting issue. Avoid disputes over namespaces, which can take massive amounts of time and resources to manage. Guarantee that namespaces are unique, solving the issue of "clashing" namespaces. Non-unique namespaces work poorly and cause disputes, as demonstrated by …

> [@Namespacing on Crates.io](https://internals.rust-lang.org/t/namespacing-on-crates-io/8571):
>
> I am moving this discussion here from [this thread about a Crates.io incident](https://internals.rust-lang.org/t/crates-io-incident-2018-10-15/8568/6). Why did Cargo/Crates.io choose not to namespace crates by author/organization name? Squatting for crate names would not be feasible if there was user/org namespacing. I’m not pointing fingers, pushing blame, or asking anybody to change, I’d just like to understand what the reason was for choosing not to namespace.

> **[Login or Sign Up - Dropbox](https://paper.dropbox.com/doc/All-hands-Crate-grouping-Namespacing-discussion-NEpWAaDdNuUheLpawETYT)**
>
> Login to Dropbox. Bring your photos, docs, and videos anywhere and keep your files safe.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [November 18, 2019, 7:51pm UTC](https://internals.rust-lang.org/t/pre-rfc-idea-cratespaces-crates-as-namespace-take-2-or-3/11320/2 "2019-11-18T19:51:10Z")

</div>

Another related but distinct potential feature is internal-only crates: publish a crate with crate dependencies that aren't visible to the outside world at all. Then the crate split becomes merely an implementation detail.

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [November 18, 2019, 11:26pm UTC](https://internals.rust-lang.org/t/pre-rfc-idea-cratespaces-crates-as-namespace-take-2-or-3/11320/3 "2019-11-18T23:26:45Z")

</div>

How does that work on the network level? When you download `cratespace.crate`, does that tarball include source code for all of its subcrates, or does cargo make separate requests for `subcrate.crate`s?

Are cratespaces the same thing as Cargo workspaces? How does that grouping work locally before publishing?

---

<div class="post-metadata">

**Author:** ![samsieber](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/samsieber/32/4671_2.png) [@samsieber](https://internals.rust-lang.org/u/samsieber)\
**Post date:** [November 21, 2019, 3:59pm UTC](https://internals.rust-lang.org/t/pre-rfc-idea-cratespaces-crates-as-namespace-take-2-or-3/11320/4 "2019-11-21T15:59:40Z")

</div>

I haven't thought too hard about these points, so hopefully my answers make sense 🙂

> [@kornel](#):
>
> When you download `cratespace.crate` , does that tarball include source code for all of its subcrates, or does cargo make separate requests for `subcrate.crate` s?

I'd be fine with it making separate requests for each.

> [@kornel](#):
>
> Are cratespaces the same thing as Cargo workspaces? How does that grouping work locally before publishing?

I envisioned it working as the cratespace root crate listing what subcrates it contains and each subcrate listing which cratespace it belongs to, in order to support crates that aren't in the same workspace, which means the cratespace root and its subcrates would be published separately. And when only one of those is done, the status of the subcrate would be "pending" so to speak, in the [crates.io](http://crates.io) UI. You could see what the person was wanting to happen, but it has to be approved through both.

That said, I think it would be nice to simplify it for workspaces. It'd be nice to add a flag to workspace to make it into a cratespace, without any extra work necessary, and then publishing tries to publish any crates that have changes.

---

<div class="post-metadata">

**Author:** ![Cazadorro](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cazadorro/32/4551_2.png) [@Cazadorro](https://internals.rust-lang.org/u/Cazadorro)\
**Post date:** [November 21, 2019, 4:52pm UTC](https://internals.rust-lang.org/t/pre-rfc-idea-cratespaces-crates-as-namespace-take-2-or-3/11320/5 "2019-11-21T16:52:14Z")

</div>

Does this idea have a thread/rfc/github issue etc?

---

<div class="post-metadata">

**Author:** ![carols10cents](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/carols10cents/32/98_2.png) [@carols10cents](https://internals.rust-lang.org/u/carols10cents)\
**Post date:** [September 29, 2022, 3:44pm UTC](https://internals.rust-lang.org/t/pre-rfc-idea-cratespaces-crates-as-namespace-take-2-or-3/11320/6 "2022-09-29T15:44:52Z")

</div>

> <https://github.com/rust-lang/rfcs/pull/3243>
>
> :desktop\_computer: \[Rendered\](https://github.com/Manishearth/rfcs/blob/namespaci…ng/text/0000-packages-as-optional-namespaces.md) :desktop\_computer:
> 
> 
> 
> \----
> 
> \_This was previously discussed as a pre-RFC \[here\](https://internals.rust-lang.org/t/pre-rfc-packages-as-optional-namespaces/13059), and later iterated upon in \[this repo\](https://github.com/Manishearth/namespacing-rfc).\_
> 
> \*\*There is a prototype of this RFC available on https://cratespaces.integer32.com/ with instructions \[here\](https://github.com/Manishearth/namespacing-rfc/issues/10)\*\*
> 
> This RFC brings forth a long-discussed and desired feature: some kind of namespacing for crates.io, achieved via treating "toplevel" packages themselves as namespaces, with ownership being inherited.
> 
> 
> 
> \### Please read before participating
> 
> I'm going to include some of the same disclaimers I included \[in the pre-rfc\](https://internals.rust-lang.org/t/pre-rfc-packages-as-optional-namespaces/13059):
> 
> The general topic of namespacing is one that has been discussed many times. In my understanding, a \_lot\_ of the tension here comes from people having different problems they wish to solve, and these discussions not always acknowledging that, leading to people feeling unheard.
> 
> This proposal comes out of many years of one on one discussion, reading threads, and incubation. I do think this is a viable and good path forward, but for this to work out we need to be our best selves here.
> 
> I would like to request everyone to keep this discussion constructive and respectful. If a discussion is getting super involved, it may be worth opening a discussion issue on https://github.com/Manishearth/namespacing-rfc/ and linking to it instead.
> 
> This RFC is attempting to solve the problem of making crate ownership clear in terms of other crates; it is \_not\_ attempting to solve the general problem around the squatting of root crate names. As such, if a crate can be published today under a particular name by anyone, it would also also be publishable under the same name by anyone in the world of this RFC proposal. I would prefer if we did not spend much time discussing what I see as the radically different problem of squatting in general.
> 
> Thank you all in advance for what I hope will be a constructive and fruitful RFC!
> 
> 
> Update: Discussions about separator choice are being conducted \[here\](https://github.com/Manishearth/namespacing-rfc/issues/1).

---

<div class="post-metadata">

**Author:** ![simonbuchan](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/simonbuchan/32/9390_2.png) [@simonbuchan](https://internals.rust-lang.org/u/simonbuchan)\
**Post date:** [October 7, 2022, 12:37pm UTC](https://internals.rust-lang.org/t/pre-rfc-idea-cratespaces-crates-as-namespace-take-2-or-3/11320/7 "2022-10-07T12:37:47Z")

</div>

What's the functional difference with the current practice of a parent crate with a big fat list of `pub use foo_bar as bar`? Discoverability on [crates.io](http://crates.io)?

---

<div class="post-metadata">

**Author:** ![comex](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/comex/32/2587_2.png) [@comex](https://internals.rust-lang.org/u/comex)\
**Post date:** [October 7, 2022, 6:41pm UTC](https://internals.rust-lang.org/t/pre-rfc-idea-cratespaces-crates-as-namespace-take-2-or-3/11320/8 "2022-10-07T18:41:54Z")

</div>

@simonbuchan This thread is old, so further discussion about the concept should probably happen somewhere else.

---

<div class="post-metadata">

**Author:** ![carols10cents](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/carols10cents/32/98_2.png) [@carols10cents](https://internals.rust-lang.org/u/carols10cents)\
**Post date:** [October 7, 2022, 6:48pm UTC](https://internals.rust-lang.org/t/pre-rfc-idea-cratespaces-crates-as-namespace-take-2-or-3/11320/9 "2022-10-07T18:48:09Z")

</div>

Closed on October 7, 2022, 6:48pm UTC


