Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zig fmt failure in Window Subsystem for Linux #2481

Closed
Carter12s opened this issue May 13, 2019 · 4 comments · Fixed by #2527
Closed

zig fmt failure in Window Subsystem for Linux #2481

Carter12s opened this issue May 13, 2019 · 4 comments · Fixed by #2527
Milestone

Comments

@Carter12s
Copy link

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?
@Carter12s
Copy link
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 😄 !

@daurnimator
Copy link
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.

@andrewrk
Copy link
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.

@andrewrk andrewrk added this to the 0.5.0 milestone May 15, 2019
@jayschwa
Copy link
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
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants