Skip to content

mime: incorrect mime-type for .js files on windows 10 (text/plain instead of application/javascript) #32350

@kjk

Description

@kjk

What version of Go are you using (go version)?

$ go version
go version go1.12.5 windows/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\kjk\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\kjk\go
set GOPROXY=
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\Users\kjk\src\apps\offdocs\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\kjk\AppData\Local\Temp\go-build840166758=/tmp/go-build -gno-record-gcc-switches

What did you do?

Consider this program:

package main

import (
	"fmt"
	"mime"
)

func main() {
	ct := mime.TypeByExtension(".js")
	fmt.Printf("ct: %s\n", ct)
}

https://play.golang.org/p/JTVjWc9xMDE

What did you expect to see?

Mime-type for .js files should be application/javascript on all platforms.

What did you see instead?

On Linux it returns ct: application/javascript

On my latest Windows 10 it returns ct: text/plain; charset=utf-8

This also affects Content-Type returned by http.ServeFile() for .js files.

This is because in src\mime\type.go:

func initMime() {
	if fn := testInitMime; fn != nil {
		fn()
	} else {
		setMimeTypes(builtinTypesLower, builtinTypesLower)
		osInitMime()
	}
}

On Windows osInitMime() reads info for additional extensions from registry and over-writes mime-type for .js files.

Note: it's possible this won't repro on every version on Windows.

Reversing the initialization order should fix it:

osInitMime()
setMimeTypes(builtinTypesLower, builtinTypesLower)

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.OS-Windows

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions