Skip to content

zig fmt failure in Window Subsystem for Linux #2481

Closed

Description

@Carter12s

I'm new to zig so this may be doing something stupid, but it appears that zig fmt has an issue when running within WSL.

System Config:
Windows 10 Version 10.0.17134 Build 17134
Running Ubuntu 18.04.1 via WSL

with a basic hello-world file:

V 0.4.0

carter@Carter-Desktop:~/zig-test/src$ zig fmt hello.zig                                                                                                      
hello.zig                                                                                                                                                    
error: Unexpected                                                                                                                                            
/home/carter/zig-linux-x86_64-0.4.0/lib/zig/std/os.zig:2248:5: 0x24a541 in ??? (fmt)                                                                             
return error.Unexpected;                                                                                                                                     
^                                                                                                                                                        
/home/carter/zig-linux-x86_64-0.4.0/lib/zig/std/os.zig:1221:21: 0x29323d in ??? (fmt)                                                                                    
else => return unexpectedErrorPosix(err),                                                                                                                            
^                                                                                                                                        
/home/carter/zig-linux-x86_64-0.4.0/lib/zig/std/os.zig:1183:13: 0x2931bb in ??? (fmt)                                                                                    
return renameC(&self.tmp_path_buf, &dest_path_c);                                                                                                            
^                                                                                                                                                
/home/carter/zig-linux-x86_64-0.4.0/lib/zig/std/io.zig:1012:9: 0x279f2d in ??? (fmt)                                                                                 
try self.atomic_file.finish();                                                                                                                               
^                                                                                                                                                    
/home/carter/zig-linux-x86_64-0.4.0/lib/zig/std/special/fmt_runner.zig:199:13: 0x257429 in ??? 
(fmt)                                                                     
try baf.finish();                                                                                                                                            
^                                                                                                                                                
/home/carter/zig-linux-x86_64-0.4.0/lib/zig/std/special/fmt_runner.zig:112:9: 0x253df5 in ??? (fmt)                                                                  
try fmtPath(&fmt, file_path, check_mode); 

V latest

carter@Carter-Desktop:~/zig-test/src$ zig fmt ./hello.zig                                                                                                    
./hello.zig                                                                                                                                                  
error: Unexpected                                                                                                                                            
/home/carter/zig-linux-x86_64-0.4.0/lib/zig/std/os.zig:2248:5: 0x24a541 in ??? (fmt)                                                                             
return error.Unexpected;                                                                                                                                     
^                                                                                                                                                        
/home/carter/zig-linux-x86_64-0.4.0/lib/zig/std/os.zig:1221:21: 0x29323d in ??? (fmt)                                                                                    
else => return unexpectedErrorPosix(err),                                                                                                                            
^                                                                                                                                        
/home/carter/zig-linux-x86_64-0.4.0/lib/zig/std/os.zig:1183:13: 0x2931bb in ??? (fmt)                                                                                    
return renameC(&self.tmp_path_buf, &dest_path_c);                                                                                                            
^                                                                                                                                                
/home/carter/zig-linux-x86_64-0.4.0/lib/zig/std/io.zig:1012:9: 0x279f2d in ??? (fmt)                                                                                 
try self.atomic_file.finish();                                                                                                                               
^                                                                                                                                                    
/home/carter/zig-linux-x86_64-0.4.0/lib/zig/std/special/fmt_runner.zig:199:13: 0x257429 in ??? 
 (fmt)
                                                                     try baf.finish();                                                                                                                                            
^                                                                                                                                                
/home/carter/zig-linux-x86_64-0.4.0/lib/zig/std/special/fmt_runner.zig:112:9: 0x253df5 in ?? (fmt)
                                                                  try fmtPath(&fmt, file_path, check_mode);                                                                                                                    
^                                       

Zig fmt is producing a cache file which appears to contain the desired contents alongside the file:

carter@Carter-Desktop:~/zig-test/src$ ls                                                                                                                     
K-IIxPOeJ_t0r6Rp  hello.zig                                                                                                                                  
carter@Carter-Desktop:~/zig-test/src$ cat K-IIxPOeJ_t0r6Rp                                                                                                   
const std = @import("std");                                                                                                                                  
/// I'm writing a doc comment in zig                                                                                                                         
//I'm writing a regular comment in zig                                                                                                                       
pub fn main() void {                                                                                                                                             
std.debug.warn("Hello, world!\n");                                                                                                                       
} 

I've done some initial debugging by adding the following snippet in os.zig:

1219             posix.ENOTEMPTY => return error.PathAlreadyExists,                                                                                          
1220             posix.EROFS => return error.ReadOnlyFileSystem,                                                                                             
1221             posix.EXDEV => return error.RenameAcrossMountPoints,                                                                                        
1222             else => {                                                                                                                                   
1223              std.debug.warn("Err: val {}", err);                                                                                                        
1224              return unexpectedErrorPosix(err);                                                                                                          
1225             },                                                                                                                                          
1226         }                                                                                                                                               
1227     }    

which tells me that the error number is 38.

Which is apparently:

#define ENOSYS          38      /* Function not implemented */

After some digging it appears that os/linux.zig is using renameat(2) which is not implemented on WSL. According to this other issue yuk7/ArchWSL#5

I do not know enough about the POSIX standards to tell if Zig is using a syscall which should not be expected to be universal across all Linux instances, or if WSL is out of spec (I suspect that latter).

I'm new to Zig, but it seems reasonable that following changes could be made:

  • ENOSYS should be added to the renameC switch statement so that the error message is clearer about the source, and nobody has to add a print statement to see the posix errno.
  • It is strange to me that this crash left fragment files in my workspace. I would expect Zig to cleanup the fragment file if an error were encountered here
  • It appears that using a more basic rename syscall may be supported on WSL? May be a performance degradation, but would improve compatibility. Is there a way would could check if the syscall is available and gracefully fallback?

Activity

  1. Carter12s commented on May 13, 2019

    @Carter12s
    Author

    I'd be happy to take a crack at writing an MR for any of this, but again this is literally my first day using zig 😄 !

  2. daurnimator commented on May 13, 2019

    @daurnimator
    Contributor

    The lack of renameat in WSL is a missing feature on their side.
    However it's also missing in older linux kernels, which makes this issue related to #1907

    You could add a case to std/os/linux.zig so that (on architectures that support it: not all architectures have the old non-at syscalls) the old rename syscall is used by rename.

  3. andrewrk commented on May 13, 2019

    @andrewrk
    Member

    Is anything lost by simply using the older syscall?

    The set of syscalls available isn't consistent; for example some architectures only have the newer syscall. I've been meaning to audit this commit: 4a8c992 but haven't done it yet - will do it with #2380. std.os.linux.rename will always be the rename syscall, but the posix API layer will try multiple syscalls if it gets ENOSYS.

  4. added this to the 0.5.0 milestone on May 15, 2019
  5. jayschwa commented on May 15, 2019

    @jayschwa
    Contributor

    Microsoft is going to be shipping a Linux kernel with full system call support in their next version of WSL. This issue might be made obsolete once that ships.

    https://devblogs.microsoft.com/commandline/announcing-wsl-2/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions