Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 26, 2025

When using precompiled .slang-module files, error messages for missing imports displayed garbage characters (mojibake) instead of clean diagnostic output:

b1.slang-module(1): error 1: cannot open file 'example.slang'.
䙆٦b1.slang-module(1): error 15301: failed to find imported file 'example'
䙆٦

Root cause: IncludeSystem::loadFile creates a SourceFile with binary module content. When diagnostics are raised, the code finds this existing file and attempts to display "source lines" from binary data.

Changes:

  • slang-session.cpp: Always create a fresh empty source file for binary module diagnostic locations instead of reusing one that may contain binary content
  • slang-diagnostic-sink.cpp: Add hasContent() guard in _sourceLocationNoteDiagnostic and _tokenLengthNoteDiagnostic to skip source line display when content isn't available

After fix:

b1.slang-module(1): error 1: cannot open file 'example.slang'.
b1.slang-module(1): error 15301: failed to find imported file 'example'
Original prompt

This section details on the original issue you should resolve

<issue_title>Slang prints mojibake when a .slang-module can't find a source file</issue_title>
<issue_description># Issue Description
When using precompiled .slang-module files, the error messages printed end in mojibake, like this:

b1.slang-module(1): error 1: cannot open file 'example.slang'.
䙆٦b1.slang-module(1): error 15301: failed to find imported file 'example'
䙆٦

My guess is the internal representation of a string rather than its data pointer is being printed out somewhere. Or maybe it's trying to print out source code that hasn't been loaded.

Reproducer

Download Slang 2025.23.1 and set up a folder structure like this (also attached as slang-mojibake.zip )

📄 slangc.exe
📁 a
   📄 example.slang
📁 b
   📄 b1.slang
   📄 b2.slang

where the files contain the following text:

a/example.slang:

module "example";

b/b1.slang:

module "b1";
import "example";

b/b2.slang:

import "b1";

[shader("compute")]
void main(){}

Now cd into the b folder. Slang should compile b2.slang correctly when the a folder is on the include path:

>..\slangc -I "../a" b2.slang -target spirv
; SPIR-V
; Version: 1.5
...

Now, precompile b1.slang to a slang-module:

..\slangc -I "../a" -emit-ir b1.slang -o b1.slang-module

Verify that it works when the include path is specified:

>..\slangc -I "../a" b2.slang -target spirv
; SPIR-V
; Version: 1.5
...

Now omit the include path and observe the bug:

>..\slangc b2.slang -target spirv
b1.slang-module(1): error 1: cannot open file 'example.slang'.
䙆٦b1.slang-module(1): error 15301: failed to find imported file 'example'
䙆٦

Expected Behavior

Slang should not print out mojibake. The intended error message was probably something like this:

b1.slang(3): error 1: cannot open file 'example.slang'.
import "example";
       ^~~~~~~~~
b2.slang(1): error 39999: import of module 'b1' failed because of a compilation error
import "b1";
       ^~~~
(0): fatal error 39999: compilation ceased

Actual Behavior

Slang emits mojibake. I have seen it segfault at least once due to this, but don't have a reliable repro case.

Environment

  • Slang Version (release version number or commit hash): 2025.23.1
  • OS: Windows 23H2 Build 22631.6199

Additional context

Thanks!
</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

The issue was that when loading binary modules, the IncludeSystem::loadFile
was creating a SourceFile with the binary module content. Later, when
diagnostics were raised for missing imports, the code would find this
existing SourceFile and try to display "source lines" from binary data,
resulting in mojibake.

The fix ensures that when creating a source location for a serialized
module's diagnostics, we always create a fresh empty source file rather
than reusing one that might contain binary data.

Co-authored-by: bmillsNV <163073245+bmillsNV@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix mojibake in error messages for missing source files Fix mojibake in error messages when .slang-module can't find source file Nov 26, 2025
Copilot AI requested a review from bmillsNV November 26, 2025 18:32
Copilot finished work on behalf of bmillsNV November 26, 2025 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Slang prints mojibake when a .slang-module can't find a source file

2 participants