# Error embedding file data with a macro

**URL:** <https://forums.swift.org/t/error-embedding-file-data-with-a-macro/81448>\
**Category:** Using Swift\
**Tags:** macros\
**Created:** [August 2, 2025, 3:40am UTC](https://forums.swift.org/t/error-embedding-file-data-with-a-macro/81448 "2025-08-02T03:40:42Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![awwright](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/awwright/32/40477_2.png) [@awwright](https://forums.swift.org/u/awwright)\
**Post date:** [August 2, 2025, 3:40am UTC](https://forums.swift.org/t/error-embedding-file-data-with-a-macro/81448/1 "2025-08-02T03:40:42Z")

</div>

I've written a Swift macro to substitute the contents of a file into a string. However, I get the following error:

> Failed to read file at path: .../main.swift, error: Error Domain=NSCocoaErrorDomain Code=257 "The file “main.swift” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=.../main.swift, NSURL=file://.../main.swift, NSUnderlyingError=0x600002d5d5f0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}} (from macro 'stringify')

It's reading its own file, so how am I getting a permission error? It doesn't work reading other files in the same directory, either, that I ought to have permissions to read.

Second, how do you usually embed files into an executable? (A CLI and Swift package where filesystem access may not be available, so an app package/bundle is not an option, unless there's some way bundles support embedding in compiled executables.)

---

<div class="post-metadata">

**Author:** ![KeithBauerANZ](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/keithbaueranz/32/27897_2.png) [@KeithBauerANZ](https://forums.swift.org/u/KeithBauerANZ)\
**Post date:** [August 3, 2025, 11:11pm UTC](https://forums.swift.org/t/error-embedding-file-data-with-a-macro/81448/2 "2025-08-03T23:11:53Z")

</div>

Macros are run in a sandbox which limits filesystem access, for a variety of reasons.

You could consider using a C target in your package, and utilizing the newish [`#embed` directive](https://www.phoronix.com/news/LLVM-Clang-19-C23-Embed).

---

<div class="post-metadata">

**Author:** ![awwright](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/awwright/32/40477_2.png) [@awwright](https://forums.swift.org/u/awwright)\
**Post date:** [August 4, 2025, 4:56am UTC](https://forums.swift.org/t/error-embedding-file-data-with-a-macro/81448/3 "2025-08-04T04:56:20Z")

</div>

Is there somewhere I can get more information on the sandbox? The API provides the filesystem symbols as well as full filenames, so that's something I'd expect to have access to.  
Also usually if there's limited filesystem access, this is distinctly noted, e.g. [MEVideoDecoder | Apple Developer Documentation](https://developer.apple.com/documentation/mediaextension/mevideodecoder)

---

<div class="post-metadata">

**Author:** ![RandomHashTags](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/randomhashtags/32/24259_2.png) [@RandomHashTags](https://forums.swift.org/u/RandomHashTags)\
**Post date:** [August 4, 2025, 6:08am UTC](https://forums.swift.org/t/error-embedding-file-data-with-a-macro/81448/4 "2025-08-04T06:08:25Z")

</div>

> **[Documentation](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/macros/#Macro-Expansion)**

You have to scroll down a little, but here is the quote (emphases mine):

> [@](#):
>
> Swift helps macro authors avoid accidentally reading other input by restricting the code that implements macros:
> 
> - The AST passed to a macro implementation contains only the AST elements that represent the macro, not any of the code that comes before or after
> - **The macro implementation runs in a sandboxed environment that prevents it from accessing the file system or the network.**
> 
> In addition to these safeguards, the macro’s author is responsible for not reading or modifying anything outside of the macro’s inputs. For example, a macro’s expansion must not depend on the current time of day.

Worth noting that macro expansion file system access works on Linux (on some distros; use at your own risk, but you really shouldn't).

---

<div class="post-metadata">

**Author:** ![Max\_Desiatov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/max_desiatov/32/20654_2.png) [@Max\_Desiatov](https://forums.swift.org/u/Max_Desiatov)\
**Post date:** [August 4, 2025, 1:46pm UTC](https://forums.swift.org/t/error-embedding-file-data-with-a-macro/81448/5 "2025-08-04T13:46:27Z")

</div>

> [@RandomHashTags](#):
>
> Worth noting that macro expansion file system access works on Linux (on some distros; use at your own risk, but you really shouldn't).

Yes, you absolutely shouldn't. Macros must not have side-effects, including I/O access. The build system makes an assumption that for the same set of inputs macros always provide exactly the same output. I/O is not a designated input for a macro, as it isn't deterministic and there's no way for the build system to know which exact file a macro is reading.

There are some solutions to this at a prototyping stage that would enforce macro virtualization on all platforms, e.g. [[Macros] Add support for wasm macros by kabiroberai · Pull Request #73031 · swiftlang/swift · GitHub](https://github.com/swiftlang/swift/pull/73031) and it was mentioned in [the corresponding vision for Swift evolution](https://github.com/swiftlang/swift-evolution/blob/main/visions/webassembly.md#use-cases). In this case, macros would consistently not have access to any I/O on all platforms.

---

<div class="post-metadata">

**Author:** ![awwright](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/awwright/32/40477_2.png) [@awwright](https://forums.swift.org/u/awwright)\
**Post date:** [August 26, 2025, 5:59am UTC](https://forums.swift.org/t/error-embedding-file-data-with-a-macro/81448/6 "2025-08-26T05:59:51Z")

</div>

> [@Max\_Desiatov](#):
>
> Macros must not have side-effects

Why ought it be the case that macros not have side effects? Or I/O more specifically, I don’t see how reading from the filesystem is a problem, as it’s obviously necessary for the compile step. That is, why does C get to have macros with filesystem read access but not Swift?

And granting that macros don’t get filesystem access, is there a way I can build a Swift-compatible object file from some source data, bypassing the Swift compiler and going directly to an LLVM frontend?

---

<div class="post-metadata">

**Author:** ![muhammad](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@muhammad](https://forums.swift.org/u/muhammad)\
**Post date:** [August 26, 2025, 10:23am UTC](https://forums.swift.org/t/error-embedding-file-data-with-a-macro/81448/7 "2025-08-26T10:23:52Z")

</div>

I also face the same problem but cant get any relible solution.

---

<div class="post-metadata">

**Author:** ![Max\_Desiatov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/max_desiatov/32/20654_2.png) [@Max\_Desiatov](https://forums.swift.org/u/Max_Desiatov)\
**Post date:** [August 26, 2025, 1:54pm UTC](https://forums.swift.org/t/error-embedding-file-data-with-a-macro/81448/8 "2025-08-26T13:54:06Z")

</div>

> [@awwright](#):
>
> Why ought it be the case that macros not have side effects? Or I/O more specifically, I don’t see how reading from the filesystem is a problem, as it’s obviously necessary for the compile step.

That was explained in my original message: source code placed on the file system is tracked by the build system, as source code file paths are known to the build system. Arbitrary side effects within Swift macros are opaque and unknown to the build system, breaking determinism and assumptions built on top of that determinism.

> [@awwright](#):
>
> That is, why does C get to have macros with filesystem read access but not Swift?

For the same reason that C gets to have unrestricted memory access and data races, but Swift does not thanks to its memory safety and strict concurrency. In the same way, macros in Swift are supposed to be free from side effects (partially enforced only on some platforms for now) to prevent build system errors and incorrect incremental builds.

> [@awwright](#):
>
> And granting that macros don’t get filesystem access, is there a way I can build a Swift-compatible object file from some source data, bypassing the Swift compiler and going directly to an LLVM frontend?

You could embed data from a module written in C or C++, but do that at your own risk as Swift build system would have no way to ensure correctness of the build.

---

<div class="post-metadata">

**Author:** ![allevato](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/allevato/32/385_2.png) [@allevato](https://forums.swift.org/u/allevato)\
**Post date:** [August 26, 2025, 2:08pm UTC](https://forums.swift.org/t/error-embedding-file-data-with-a-macro/81448/9 "2025-08-26T14:08:56Z")

</div>

It's also a potential security risk if macros have unfettered access to the file system of the build machine, where some third-party dependency could read arbitrary files and embed their contents into a binary that gets shipped somewhere.

One way to handle this, if you want macros to be able to read files, is to require that the paths to those files be passed to the compiler, and provide a specialized interface that allows macros to only access the contents of allowlisted files, instead of using raw file APIs. That provides an audit trail of sorts because the package manifest/build system have to explicitly list the files that they're permitting access to.

Those issues aside, IMO something like `#embed` would be better implemented as a compiler built-in that just reads the file contents and generates the appropriate constant data at the IR level. That would be far more efficient than a macro that generates a huge array that then has to be compiled, especially because today the compiler [performs inconsistently when doing this](https://forums.swift.org/t/consistency-of-the-objectoutliner-siloptimizer-pass/53764).

---

<div class="post-metadata">

**Author:** ![Max\_Desiatov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/max_desiatov/32/20654_2.png) [@Max\_Desiatov](https://forums.swift.org/u/Max_Desiatov)\
**Post date:** [August 26, 2025, 2:13pm UTC](https://forums.swift.org/t/error-embedding-file-data-with-a-macro/81448/10 "2025-08-26T14:13:47Z")

</div>

> [@allevato](#):
>
> IMO something like `#embed` would be better implemented as a compiler built-in

I’ll emphasise that however it could be implemented in the compiler, it has to let the build system know of the file path that was read to ensure as you said access control, but also to rebuild everything correctly when the file changed.

---

<div class="post-metadata">

**Author:** ![Cyberbeni](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cyberbeni/32/46241_2.png) [@Cyberbeni](https://forums.swift.org/u/Cyberbeni)\
**Post date:** [August 27, 2025, 8:43am UTC](https://forums.swift.org/t/error-embedding-file-data-with-a-macro/81448/11 "2025-08-27T08:43:04Z")

</div>

Just add another executable target that generates those swift files and run that target before building the main target.

Or if you want to do it the hard way, you can always just switch to cmake.

Edit: you might also be able to make this into a build tool plugin, so your generate files step is run when you use `swift build` instead of having to manually run it. You can look up examples for this, like R.swift has several plugins.

---

<div class="post-metadata">

**Author:** ![KeithBauerANZ](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/keithbaueranz/32/27897_2.png) [@KeithBauerANZ](https://forums.swift.org/u/KeithBauerANZ)\
**Post date:** [August 29, 2025, 1:52am UTC](https://forums.swift.org/t/error-embedding-file-data-with-a-macro/81448/12 "2025-08-29T01:52:41Z")

</div>

> [@Max\_Desiatov](#):
>
> I’ll emphasise that however it could be implemented in the compiler, it has to let the build system know of the file path that was read to ensure as you said access control, but also to rebuild everything correctly when the file changed.

I believe Rust deals with this by reexpanding all proc macros on every build, to check they produced the same output as last time, rather than by knowing "ahead of time". This also allows macros that have other forms of nondeterminism, eg. expand to the build timestamp (though "at what cost"!)

---

<div class="post-metadata">

**Author:** ![awwright](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/awwright/32/40477_2.png) [@awwright](https://forums.swift.org/u/awwright)\
**Post date:** [September 4, 2025, 6:28pm UTC](https://forums.swift.org/t/error-embedding-file-data-with-a-macro/81448/13 "2025-09-04T18:28:41Z")

</div>

> [@KeithBauerANZ](#):
>
> reexpanding all proc macros on every build

This doesn’t make sense to me: This seems to be the same as “re-compile the whole program every time”. The point of having the build system is being able to look at the mtime of the sources and build products, and calling the compiler only if the sources are younger than the build. Could you elaborate?

---

<div class="post-metadata">

**Author:** ![awwright](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/awwright/32/40477_2.png) [@awwright](https://forums.swift.org/u/awwright)\
**Post date:** [September 4, 2025, 6:41pm UTC](https://forums.swift.org/t/error-embedding-file-data-with-a-macro/81448/14 "2025-09-04T18:41:08Z")

</div>

> [@allevato](#):
>
> It's also a potential security risk if macros have unfettered access to the file system of the build machine, where some third-party dependency could read arbitrary files and embed their contents into a binary that gets shipped somewhere.

The filesystem already provides a permissions system, and that ought to be set up so that the compiler has access to all of the files used to build the project, and the ability to write the build products (or output to stdout), and not any more.

> [@allevato](#):
>
> instead of using raw file APIs

There’s no need to provide a parallel API that does the same thing as the native file APIs… That said, I agree there ought to be a way to register which files were used by the compiler and for what purpose, so the build system can decide what future conditions (file changes, etc) require a subsequent call to the compiler.

Interestingly, the LLMs seem convinced that such an API already exists, but this appears to be a hallucination.

> [@allevato](#):
>
> Those issues aside, IMO something like `#embed` would be better implemented as a compiler built-in that just reads the file contents and generates the appropriate constant data at the IR level.

This is a necessary step, but it isn’t completely sufficient; I’m specifically looking for a way to read a file, turn that into a struct or an instance of a struct at compile time. This could be done with a mix of your `#embed` macro and a compiler directive that specifies a value is a compile-time constant, the same way that `1 + 1` is supposed to be resolved to `2` at compile time, but I’m looking for a way to guarantee this is done at compile time and not run time.

---

<div class="post-metadata">

**Author:** ![Cyberbeni](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cyberbeni/32/46241_2.png) [@Cyberbeni](https://forums.swift.org/u/Cyberbeni)\
**Post date:** [September 4, 2025, 6:41pm UTC](https://forums.swift.org/t/error-embedding-file-data-with-a-macro/81448/15 "2025-09-04T18:41:18Z")

</div>

This one cracked me up. I had to add that check to my swiftformat wrapper because `swift build` takes multiple seconds even when there is nothing to do: [MultiArchSwiftDockerfileExample/scripts/format.sh at b7689f7243f1b1195ac3d92d3496c9d71b370ce5 · Cyberbeni/MultiArchSwiftDockerfileExample · GitHub](https://github.com/Cyberbeni/MultiArchSwiftDockerfileExample/blob/b7689f7243f1b1195ac3d92d3496c9d71b370ce5/scripts/format.sh#L14-L27)

---

<div class="post-metadata">

**Author:** ![awwright](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/awwright/32/40477_2.png) [@awwright](https://forums.swift.org/u/awwright)\
**Post date:** [September 4, 2025, 7:09pm UTC](https://forums.swift.org/t/error-embedding-file-data-with-a-macro/81448/16 "2025-09-04T19:09:58Z")

</div>

> [@Cyberbeni](#):
>
> [MultiArchSwiftDockerfileExample/scripts/format.sh at b7689f7243f1b1195ac3d92d3496c9d71b370ce5 · Cyberbeni/MultiArchSwiftDockerfileExample · GitHub](https://github.com/Cyberbeni/MultiArchSwiftDockerfileExample/blob/b7689f7243f1b1195ac3d92d3496c9d71b370ce5/scripts/format.sh#L14-L27)

Why not use make?

---

<div class="post-metadata">

**Author:** ![Cyberbeni](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cyberbeni/32/46241_2.png) [@Cyberbeni](https://forums.swift.org/u/Cyberbeni)\
**Post date:** [September 5, 2025, 6:38am UTC](https://forums.swift.org/t/error-embedding-file-data-with-a-macro/81448/17 "2025-09-05T06:38:33Z")

</div>

The swift docker image doesn't have make. Its easier to just replicate the behavior manually than to solve the problem any other way.
