# Is this a \`swiftc\` bug? "error: module name "foo" is not a valid identifier"

**URL:** <https://forums.swift.org/t/is-this-a-swiftc-bug-error-module-name-foo-is-not-a-valid-identifier/53119>\
**Category:** Using Swift\
**Tags:** bug, xcode, swiftc\
**Created:** [October 27, 2021, 11:59pm UTC](https://forums.swift.org/t/is-this-a-swiftc-bug-error-module-name-foo-is-not-a-valid-identifier/53119 "2021-10-27T23:59:22Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlexanderM](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alexanderm/32/9781_2.png) [@AlexanderM](https://forums.swift.org/u/AlexanderM)\
**Post date:** [October 27, 2021, 11:59pm UTC](https://forums.swift.org/t/is-this-a-swiftc-bug-error-module-name-foo-is-not-a-valid-identifier/53119/1 "2021-10-27T23:59:22Z")

</div>

I already filed a radar (`rdar://FB9726200`, [OpenRadar](https://openradar.appspot.com/radar?id=5065119578980352)) for this thinking it was an Xcode issue, but now I'm wondering it it's an issue in `swiftc`.

Merely adding a file to one of my targets it makes it fail to build with:

```swift
error: module name "ca.momchilov.Demo-Privileged-Helper-App.helper" is not a valid identifier
Command CompileSwiftSources failed with a nonzero exit code

```

Unchecking the target membership box for that file makes the project build fine again (even though the product module name hasn't changed at all in doing that).

This issue is in a small dummy project, which I'd be glad to share (what would be the best way? IDK if it's a good idea to post random Google Drive download links on a forum like this). I don't know which specific reproduction steps cause the issue, though.

Is this something I should file on [bugs.swift.org](http://bugs.swift.org)?

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [October 28, 2021, 1:34am UTC](https://forums.swift.org/t/is-this-a-swiftc-bug-error-module-name-foo-is-not-a-valid-identifier/53119/2 "2021-10-28T01:34:23Z")

</div>

Is it the first Swift file in that target? And does that target have a custom MODULE\_NAME build setting? That could explain how you got into this scenario.

---

<div class="post-metadata">

**Author:** ![AlexanderM](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alexanderm/32/9781_2.png) [@AlexanderM](https://forums.swift.org/u/AlexanderM)\
**Post date:** [October 28, 2021, 12:41pm UTC](https://forums.swift.org/t/is-this-a-swiftc-bug-error-module-name-foo-is-not-a-valid-identifier/53119/3 "2021-10-28T12:41:06Z")

</div>

Hey Jordan,

I already have a Swift file in target (`main.swift`). I created it from the Xcode "command line tool" template, choosing Swift as the default language.

I have manually set my `PRODUCT_BUNDLE_IDENTIFIER`, `PRODUCT_MODULE_NAME`, and `PRODUCT_NAME`:

```swift
PRODUCT_BUNDLE_IDENTIFIER = "ca.momchilov.Demo-Privileged-Helper-App.helper";
PRODUCT_MODULE_NAME = "ca.momchilov.Demo-Privileged-Helper-App.helper";
PRODUCT_NAME = "ca.momchilov.Demo-Privileged-Helper-App.helper";

```

(As far as I understand it, all three have to be the same for a privileged helper tool.)

Though this doesn't seem to be an issue unless I add any further Swift files to the target.

Idk how safe it is to be running random Xcode projects from some random guy on the internet, but I've uploaded my source to GitLab so people can take a look. (I don't have any pre/post build scripts or anything like that)

> <https://gitlab.com/AMomchilov/public/experiments/demo-privileged-helper-app/-/blob/b9a6a00f/Demo%20Privileged%20Helper%20App.xcodeproj/project.pbxproj#L439-441>

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [October 28, 2021, 4:20pm UTC](https://forums.swift.org/t/is-this-a-swiftc-bug-error-module-name-foo-is-not-a-valid-identifier/53119/4 "2021-10-28T16:20:10Z")

</div>

Ahh, yeah, `PRODUCT_MODULE_NAME` does have to be a valid identifier. It’s weird that it’s being ignored for main.swift though. There’s a feature for single-file modules that infers the module name from the file name (sans .swift) unless it’s an invalid identifier, but that shouldn’t kick in if you explicitly provide one.

---

<div class="post-metadata">

**Author:** ![AlexanderM](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alexanderm/32/9781_2.png) [@AlexanderM](https://forums.swift.org/u/AlexanderM)\
**Post date:** [October 28, 2021, 5:00pm UTC](https://forums.swift.org/t/is-this-a-swiftc-bug-error-module-name-foo-is-not-a-valid-identifier/53119/5 "2021-10-28T17:00:58Z")

</div>

What makes my identifier invalid? I thought it was the dashes, but it appears not:

```swift
error: module name "ca.momchilov.DemoPrivilegedHelperApp.helper" is not a valid identifier
Command CompileSwiftSources failed with a nonzero exit code

```

edit: ohhhhh it has to be a valid _Swift_ identifier?

---

<div class="post-metadata">

**Author:** ![Nobody1707](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nobody1707/32/2169_2.png) [@Nobody1707](https://forums.swift.org/u/Nobody1707)\
**Post date:** [October 28, 2021, 5:05pm UTC](https://forums.swift.org/t/is-this-a-swiftc-bug-error-module-name-foo-is-not-a-valid-identifier/53119/6 "2021-10-28T17:05:47Z")

</div>

Yes, module names have to be a valid Swift identifier to allow disambiguating symbols by module.

---

<div class="post-metadata">

**Author:** ![AlexanderM](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alexanderm/32/9781_2.png) [@AlexanderM](https://forums.swift.org/u/AlexanderM)\
**Post date:** [October 28, 2021, 5:54pm UTC](https://forums.swift.org/t/is-this-a-swiftc-bug-error-module-name-foo-is-not-a-valid-identifier/53119/7 "2021-10-28T17:54:23Z")

</div>

What do you guys think about improving the diagnostic to be more specific about what exactly makes the identifier invalid?

[https://github.com/apple/swift-driver/pull/893/files#diff-22af3f561add4c394658e9249278ca06e8fd743f2139342e22217c0be0fc6210R25-R69](https://github.com/apple/swift-driver/pull/893/files#diff-22af3f561add4c394658e9249278ca06e8fd743f2139342e22217c0be0fc6210R25-R69)

---

<div class="post-metadata">

**Author:** ![AlexanderM](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alexanderm/32/9781_2.png) [@AlexanderM](https://forums.swift.org/u/AlexanderM)\
**Post date:** [October 28, 2021, 5:58pm UTC](https://forums.swift.org/t/is-this-a-swiftc-bug-error-module-name-foo-is-not-a-valid-identifier/53119/8 "2021-10-28T17:58:44Z")

</div>

I changed by module name to just `DemoPrivilegedHelperhelper`, and got:

```swift
<unknown>:0: error: cannot load module 'main' as 'DemoPrivilegedHelperhelper'

```

I nuked derived data (a well-ingrained habit.), and now it builds fine! ✅

I think [this diagnostic](https://github.com/apple/swift/blob/1b3fa26a5c0ba0a7f36814df79523e8a6afb4e1c/test/ModuleInterface/ModuleCache/WrongName.swiftinterface#L7) should also be improved. Nothing about it suggests that the module cache is the issue, and that it should be cleared.

For that matter, couldn't we just directly clear the module cache in this case?

---

<div class="post-metadata">

**Author:** ![AlexanderM](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alexanderm/32/9781_2.png) [@AlexanderM](https://forums.swift.org/u/AlexanderM)\
**Post date:** [October 28, 2021, 6:06pm UTC](https://forums.swift.org/t/is-this-a-swiftc-bug-error-module-name-foo-is-not-a-valid-identifier/53119/9 "2021-10-28T18:06:16Z")

</div>

> [@jrose](#):
>
> There’s a feature for single-file modules that infers the module name from the file name (sans .swift) unless it’s an invalid identifier, but that shouldn’t kick in if you explicitly provide one.

I think the Swift driver behaviour is correct here: [https://github.com/apple/swift-driver/blob/f9821a1b19fde0e319ff9b69049dd9aea6fa4174/Sources/SwiftDriver/Driver/Driver.swift#L2106-L2107](https://github.com/apple/swift-driver/blob/f9821a1b19fde0e319ff9b69049dd9aea6fa4174/Sources/SwiftDriver/Driver/Driver.swift#L2106-L2107)

The issue resides in Xcode. When there's more than the `main.swift` file in the target, the `swiftc` invocation contains:

```swift
-module-name DemoPrivilegedHelperhelper

```

But when I remove the second file, it uses:

```swift
-module-name main

```

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [October 30, 2021, 6:58pm UTC](https://forums.swift.org/t/is-this-a-swiftc-bug-error-module-name-foo-is-not-a-valid-identifier/53119/10 "2021-10-30T18:58:32Z")

</div>

Hrm, sounds like Xcode is manually doing the same thing the compiler is doing, but it shouldn’t do that when the module name is customized. Worth filing a Feedback!

Changing the diagnostic seems like a good idea; it clearly wasn’t clear enough.

I’m not sure why the module cache mattered in this case. I guess that could be a _separate_ Feedback. (I’m not convinced it’s something you could see with just the compiler or SwiftPM.)
