dragon-importsで爆速goimports生活 - プロジェクト Go modules -/dragon-imports

dragon-importsで爆速goimports生活 - プロジェクト Go modules -/dragon-imports

Fukuoka.go#13+Okayama.go #fukuokago
https://fukuokago.connpass.com/event/112073/

Cd3d2cb2dadf5488935fe0ddaea7938a?s=128

monochromegane

February 12, 2019
Tweet

Transcript

  1. - ϓϩδΣΫτ Go modules - ࡾ୐༔հ / Pepabo R&D Institute,

    GMO Pepabo, Inc. 2019.02.12 Fukuoka.go#13+Okayama.go dragon-importsͰ ര଎goimportsੜ׆
  2. ϓϦϯγύϧΤϯδχΞ ࡾ୐༔հ!NPOPDISPNFHBOF (.0ϖύϘגࣜձࣾϖύϘݚڀॴ IUUQTCMPHNPOPDISPNFHBOFDPN

  3. 1. Motivation 2. Problem of goimports 3. Approach by dragon-imports

    4. Evalution 5. Discussion about Go modules 3 ໨࣍
  4. 1. Motivation

  5. ͓·͑͸ ࠓ·ͰΠϯϙʔτͨ͠ ίʔυͷ਺Λ ͓΅͍͑ͯΔͷ͔ʁ

  6. • goimports͸importߦͷߋ৽ΛࣗಈͰߦͬͯ͘ΕΔͨΊඇৗʹศར • goimports͸։ൃதɼԿ౓΋࣮ߦ͞ΕΔ • ୳ࡧର৅ͷύοέʔδ͕૿͑Δͱ࣮ߦ͕஗͘ͳΓੜ࢈ੑ͕Լ͕Δ • ಛʹghq+pecoͰϩʔΧϧϦϙδτϦΛ౷Ұ؅ཧ͍ͯ͠Δ৔߹ʹݦஶ • ͦ͜Ͱ୳ࡧର৅ͷύοέʔδ͕૿͑ͯ΋࣮ߦ଎౓͕มΘΒͳ͍άϨʔτͳ

    goimports͕ٻΊΒΕΔ 6 goimportsͷ௿଎ԽΛղܾ͍ͨ͠
  7. 2. Problem of goimports

  8. • ର৅ͷίʔυΛASTʹม׵͠importߦΛߋ৽ɼ௥Ճɼ࡟আɽͦͷޙɼϑΥʔ ϚοτࡁΈίʔυΛग़ྗ͢Δ • ୳ࡧର৅ͷύοέʔδʹ͸ඪ४ϥΠϒϥϦͱ֎෦ύοέʔδ͕͋Δ • $GOPATH/src, $GOPATH/pkg/mod • ର৅ͷೖྗதͷύοέʔδ໊ɼγϯϘϧ໊ͷ૊Έ߹ΘͤΛશͯຬͨ͢Πϯϙʔ

    τύεΛղܾ͢Δ • math/rand (import path), rand (package name), Int (symbol) 8 goimportsͷجຊ
  9. 9 goimportsͷ࣮ߦྫ $ cat imp.txt package imp func hoge() {

    fmt.Println("hello.") kaburaya.Recorder } $ goimports imp.txt package imp import ( "fmt" "github.com/monochromegane/kaburaya" ) func hoge() { fmt.Println("hello.") kaburaya.Recorder }
  10. • ΑΓਖ਼͘͠͸୳ࡧର৅ͱͳΔʮ֎෦ʯύοέʔδ͕૿Ճͨ͠৔߹ʹ஗͘ͳΔ • ΫϥΠΞϯτ؀ڥ΍࣌ؒܦաʹΑͬͯ֎෦ύοέʔδҰཡ͸มԽ͢Δ͜ͱ͔Β౎౓ɼ୳ࡧ ର৅ͷσΟϨΫτϦ୳ࡧɼߏจղੳɼϦϑΝϨϯεͷநग़Λߦ͏ඞཁ͕͋ΔͨΊɽ • ൓ରʹɼύοέʔδҰཡ͕؀ڥʹΑͬͯมԽ͠ͳ͍ඪ४ύοέʔδ͸Πϯϙʔ τύεɼύοέʔδ໊ɼγϯϘϧͷϦετΛࣄલʹղੳ͞Ε͍ͯΔ 10 ͳͥgoimports͕஗͘ͳΔͷ͔

  11. goimportsඪ४ύοέʔδ 11 package imports var stdlib = map[string]map[string]bool{ "archive/tar": map[string]bool{

    "ErrFieldTooLong": true, "ErrHeader": true, "ErrWriteAfterClose": true, "ErrWriteTooLong": true, $GOROOT/api/go1.*txt golang.org/x/tools/imports/mkstdlib.go golang.org/x/tools/imports/zstdlib.go golang.org/x/tools/cmd/goimports.go#main golang.org/x/tools/imports/imports.go#Process golang.org/x/tools/imports/fix.go#addStdlibCandidates pkg crypto/rsa, method (*PrivateKey) Size() int pkg crypto/rsa, method (*PublicKey) Size() int
  12. • First pass (Πϯϙʔτ͕ඞཁ͔ௐ΂Δ) • ର৅಺ͷϦϑΝϨϯεҰཡͱimportߦ͕߹க͢Δ͔֬ೝ • Second pass (ඪ४ύοέʔδ·ͨ͸طimportߦ͔Βղܾ)

    • var stdlib·ͨ͸ଞϑΝΠϧͰimportࡁΈͷߦΛީิͱͯ͠߹க͢Δ͔֬ೝ • Third pass (֎෦ύοέʔδ͔Βղܾ) • $GOPATH/src΋͘͠͸$GOPATH/pkg/mod͔ΒϦϑΝϨϯεʹࣅͨσΟϨΫτϦΛߜΓࠐ ΈɼީิϑΝΠϧΛղੳͯ֬͠ೝ 12 goimportsͷߴ଎Խͷ޻෉
  13. 13 ஈ֊͝ͱͷ࣮ߦ଎౓ TUQBTT ЖT OEQBTT ЖT SEQBTT ЖT Πϯϙʔτෆཁ 

      ඪ४ϥΠϒϥϦͷΤΫεϙʔτΛࢀর    ଞϑΝΠϧͷΠϯϙʔτΛࢀর    ֎෦ύοέʔδͷΤΫεϙʔτΛࢀর      $ tree !"" imp.txt !"" other.go #"" other2.go $ find . -type d | wc -l 120840 Original goimports
  14. 3. Approach by dragon-imports

  15. • ΫϥΠΞϯτ؀ڥ͝ͱʹ͸୳ࡧର৅ͷύοέʔδҰཡ͸ਓؒ࣌ؒͰ͔͠มԽ͠ ͳ͍ • ࣄલղੳͷ݁Ռ͕ݱঢ়ʹԊΘͳ͚Ε͹શ୳ࡧΛߦ͏ϑΥʔϧόοΫ͕Մೳ • ͞Βʹ͸ΠϯϙʔτߦʹޡΓ͕͋ͬͯ΋ϏϧυͰࣦഊ͢Δ • ͜ΕΒͷલఏ͔Β֤ΫϥΠΞϯτ؀ڥ͝ͱʹ୳ࡧର৅ͷύοέʔδΛ೚ҙͷλ ΠϛϯάͰࣄલղੳ͓ͯ͘͜͠ͱͰgoimportsͷߴ଎Խ͕Մೳ

    15 ఏҊख๏ͷண؟఺
  16. • monochromegane/dragon-imports • A tool for speedup go imports command

    • https://github.com/monochromegane/dragon-imports • ඪ४ύοέʔδͱ$GOPATH/srcΛղੳ͠ɼzstdlib.goΛ࠶ੜ੒ɼgoimportsͷ ࠶Πϯετʔϧ·Ͱߦ͏ • zstdlib.go(=var stdlib)ͷ෼཭౓ͷߴ͞ʹΑΓ௥ैੑ΍൚༻ੑ͕ߴ·Δ 16 ఏҊख๏ͷ࣮૷ $ go get github.com/monochromegane/dragon-imports/... $ dragon-imports
  17. dragon-imports 17 package imports var stdlib = map[string]map[string]bool{ "archive/tar": map[string]bool{

    "ErrFieldTooLong": true, "ErrHeader": true, "ErrWriteAfterClose": true, "ErrWriteTooLong": true, $GOROOT/api/go1.*txt dragon-imports golang.org/x/tools/imports/zstdlib.go golang.org/x/tools/cmd/goimports.go#main golang.org/x/tools/imports/imports.go#Process golang.org/x/tools/imports/fix.go#addStdlibCandidates $GOPATH/**/*.go
  18. 4. Evalution

  19. 19 ࣮ߦ࣌ؒͷൺֱ TUQBTT ЖT OEQBTT ЖT SEQBTT ЖT Πϯϙʔτෆཁ 

      ඪ४ϥΠϒϥϦͷΤΫεϙʔτΛࢀর    ଞϑΝΠϧͷΠϯϙʔτΛࢀর    ֎෦ύοέʔδͷΤΫεϙʔτΛࢀর      Original goimports TUQBTT ЖT OEQBTT ЖT SEQBTT ЖT Πϯϙʔτෆཁ    ඪ४ϥΠϒϥϦͷΤΫεϙʔτΛࢀর    ଞϑΝΠϧͷΠϯϙʔτΛࢀর    ֎෦ύοέʔδͷΤΫεϙʔτΛࢀর    Dragonize goimports
  20. 5. Discussion about Go modules

  21. • Go modulesΛ༗ޮʹ͢Δͱ$GOPATH/srcͰ͸ͳ͘$GOPATH/pkg/mod഑Լ ͷόʔδϣχϯά؅ཧ͞Εͨύοέʔδ͕ར༻͞ΕΔ • goimportsͰ΋୳ࡧର৅Λಉ͡Α͏ʹ੾Γସ͍͑ͯΔ • ֎෦ύοέʔδʹόʔδϣχϯάͷ֓೦͕ಋೖ͞ΕΔͨΊɼಉҰΠϯϙʔτύ εͰ͋ͬͯ΋γϯϘϧϦετ͕ҟͳΔՄೳੑ͕͋Δʢͨͩ͠ϝδϟʔόʔδϣ ϯ͕ҟͳΔͱ/v2ͷΑ͏ʹผͱͯ͠ѻ͑Δʣ

    • ඪ४ύοέʔδ͸όʔδϣχϯάͷ֓೦͕·ͩͳ͍ͨΊzstdlib.go͸ෳ਺όʔ δϣϯͷγϯϘϧΛѻ͏ߏ଄ʹͳ͍ͬͯͳ͍ 21 Go modulesΛͲ͏ѻ͏͔
  22. • Go modules؅ཧͷ֎෦ύοέʔδΛࣄલղੳ͢Δ • ΦϓγϣϯʹΑͬͯղੳର৅ΛGOPATH/pkg/modʹมߋ͢ΔɽGO111MODULES=autoͷ ڍಈ͸ա౉ظͰ͋Δ͜ͱɼzstdlib.go͸WorkingDirΛ஌Εͳ͍͜ͱ͔ΒରԠ͠ͳ͍ • ֎෦ύοέʔδ͸֤ϝδϟʔόʔδϣϯ಺ͷ࠷৽ͷΈΛࣄલղੳ͢Δ • ϚΠφʔόʔδϣϯҎԼͷෳ਺όʔδϣϯ͕ଘࡏ͢Δ৔߹ɼbreaking

    change͸ͳ͍લఏͰ ඪ४ύοέʔδͱಉ༷ʹ࠷৽ͷΈରԠɽsemverະରԠͳ֎෦ύοέʔδʹ͓͍ͯ΋ຊํ਑ ʹΑͬͯޡ൑ఆ๷ࢭɼ࠷৽Խ΁ͷಈػ෇͚ͱ͢Δ • vendoring͸ࣄલղੳ͠ͳ͍ • ͦ΋ͦ΋ڞ௨Խ͞Εͨύοέʔδ܈͕૿େ͢Δ͜ͱͰҾ͖ى͜͞ΕΔ஗ԆͰ͋ΓɼϓϩδΣ Ϋτ͝ͱͷ࠷খͷύοέʔδ୳ࡧʹΑΔ஗Ԇ͸ߟ͑ʹ͍ͨ͘Ί 22 Go modulesΛͲ͏ѻ͏͔
  23. 23 dragon-imports $ go get -u github.com/monochromegane/dragon-imports/... $ dragon-imports --gomodules

    • gomodulesΦϓγϣϯΛ࣮ݧతʹఏڙ • Go1.12ͷਖ਼ࣜαϙʔτͷঢ়گΛݟͯΦϓγϣϯॳظ஋Λtrueͱ͢Δ༧ఆ • ܅΋ࠓ͙͢go get -u !!!
  24. • Go modulesͰ͸v2Ҏ߱ͷϝδϟʔόʔδϣϯ͝ͱʹΠϯϙʔτύε͕ҟͳΔ • ͔͠͠ύοέʔδύε͸ϝδϟʔόʔδϣϯΛؚ·ͳ͍ 24 v2ͱ͍͏ࢗ٬ $ tree !""

    hellomod $ #"" v2@v2.0.0 #"" hellomod@v1.0.1 package main import "github.com/donvito/hellomod/v2" func main() { hellomod.SayHello("Melvin") } • ඪ४ϥΠϒϥϦ͸όʔδϣϯΛߟྀ͠ͳ͍ͨΊ “v2”ͱ“hellomod”Λൺֱ͞Ε ͯ͠·͍ɼ֎෦ύοέʔδ୳ࡧ͕࢝·ͬͯ͠·͏……………………………!!!
  25. R.I.P.

  26. • dragon-importsͷ࢓૊ΈΛ঺հͨ͠ • UsefulͳΕͲGo modules࣌୅ʹ͸׬શରԠ͸ݫͦ͠͏ • ७ਮʹύοέʔδ͚ͩͷσΟϨΫτϦͱͳΔͷͰ஗Ԇ΋ൃੜ͠ͳ͍ͱࢥΘΕ Δɽ͠͹Β͘ӡ༻ͯ͠ࠔͬͨΒdragon-import.v2Λߟ͑Δɽ • Go

    modules࣌୅ͷศརπʔϧ΋࡞͍ͬͯ͜͏ • ͱΓ͋͑ͣGo modulesʹدͤΔʹ౰ͨͬͯҰཡΛऔಘ͢ΔͨΊͷ
 ’mole(౔ཽ)’Λ࡞ͬͯΔ https://github.com/monochromegane/mole 26 ·ͱΊ
  27. None